I don't know what you mean by "when it's outside a variable"
You seem to like to use variables in the global scope. These are variables available to all functions. The first time you declare a variable like that you should use the var keyword:
Code:
var myvariable = 'whatever';
At least that way it won't conflict with tags on the page that have an id of the same name. This only happens in IE where document.all is assumed if a variable isn't formally declared with the var keyword. But when it does happen, it can often be very hard to figure out and fix.
A better overall practice is to use no global variables or only one. Code can be constructed that way and it makes conflicts with other scripts virtually impossible.
Here's the same code with only one global variable (mydatedata):
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
// Date/Time
var mydatedata;
(function(){
var currentDate = new Date(),
day = currentDate.getDate(),
month = currentDate.getMonth() + 1,
year = currentDate.getFullYear(),
currentTime = new Date(),
hours = currentTime.getHours(),
minutes = currentTime.getMinutes(),
suffix = "AM";
if (day < 10) {
day = '0' + day;
}
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
if (minutes < 10){
minutes = "0" + minutes
}
mydatedata = {
fldrtime: hours + ":" + minutes + " " + suffix,
fldrdate: day + "/" + month + "/" + year
};
})();
</script>
</head>
<body>
<script type="text/javascript">
alert(mydatedata.fldrdate);
</script>
</body>
</html>
Bookmarks