PDA

View Full Version : Resolved javascript trouble with varibles (date)



keyboard
02-05-2012, 05:18 AM
Hello everybody,
I have this code


// Date/Time
currentDate = new Date()
day = currentDate.getDate()

if (day < "10") {
zero = "0"
day = ( Troublesom Spot);
}


Where troublesom spot is, I would like to have 0 and then the day variable so that if the date is less than ten, it would display like this.

Instead of 5/2/2012
05/2/2012


I'm sorry if I haven't been clear enough,
Keyboard1333

jscheuer1
02-05-2012, 05:33 AM
if (day < 10) {
day = '0' + day;
}

keyboard
02-05-2012, 06:30 AM
I'm still getting the same problem. Here's my full code.

index.html


<html>
<head>
<script language="JavaScript" src="function.js"></script>
<script language="JavaScript" src="settings.js"></script>
</head>
<body>
<script language="JavaScript">
alert(fldrdate);
</script>
<br />
<script language="JavaScript">
</script>
</body>
</html>


function.js


// Date/Time
currentDate = new Date()
day = currentDate.getDate()
if (day < 10) {
day = '0' + day;
}
month = currentDate.getMonth() + 1
year = currentDate.getFullYear()
currentTime = new Date()
hours = currentTime.getHours()
minutes = currentTime.getMinutes()
suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
if (minutes < 10)
minutes = "0" + minutes


fldrtime = (+ hours + ":" + minutes + " " + suffix + "")
fldrdate = (+ day + "/" + month + "/" + year + "")




Settings.js is currently empty.

Is it something to do with the alert -


<script language="JavaScript">
alert(fldrdate);
</script>

jscheuer1
02-05-2012, 08:29 AM
No, it's here:


fldrdate = (+ day + "/" + month + "/" + year + "")

Because that red + isn't concatenating day to a preceding string, it's signing the day value. Signing a string turns it into a number*. Numbers preceded with 0's are stripped of those zeros. It's the same as doing:


fldrdate = ((1 * day) + "/" + month + "/" + year + "")

You can do:


fldrdate = ("" + day + "/" + month + "/" + year + "")

But it's more common to simply:


fldrdate = (day + "/" + month + "/" + year + "")

Or (parentheses are not required here and adding an empty string at the end of what is already a string does nothing):


fldrdate = day + "/" + month + "/" + year;

BTW - You've got a lot of undeclared variables in the code. It's not a problem in this particular case. But as your code gets more complex, and/or as it's used on pages with more complex markup, sooner or later that practice will become the source of one or more errors and/or unexpected results.



*If it can make sense as number it's converted to the most concise expression of that number, otherwise it becomes NaN which technically, even though it literally means 'Not a Number', is still a number:


alert(typeof NaN); //alerts 'number'

keyboard
02-06-2012, 05:10 AM
Thanks heaps jscheuer1



No, it's here:
BTW - You've got a lot of undeclared variables in the code. It's not a problem in this particular case. But as your code gets more complex, and/or as it's used on pages with more complex markup, sooner or later that practice will become the source of one or more errors and/or unexpected results.


So when it's outside a function i should always use



var variable = "test"


instead of



variable = "test"

jscheuer1
02-06-2012, 05:32 AM
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:


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):


<!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>

keyboard
02-06-2012, 10:18 PM
Thanks for your reply!
Oops, I meant to write function rather than variable.



day = currentDate.getDate(),
month = currentDate.getMonth() + 1,
year = currentDate.getFullYear(),
currentTime = new Date(),
hours = currentTime.getHours(),
minutes = currentTime.getMinutes(),
suffix = "AM";


Wouldn't all these variables still be global variables because there's no var before them?

jscheuer1
02-07-2012, 01:11 AM
Ah, but there is:



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";

Because of the commas at the ends of the lines there, the variable declaration doesn't end until the semi-colon.

You can test it out by doing in the body:


<script type="text/javascript">
alert(typeof hours);
</script>

which gives 'undefined'.


Oh and your question makes perfect sense if you meant outside a function. In that case, yes - the first time you declare a global you should use the var keyword. It will avoid a possible conflict with the implied document.all collection in IE browsers*. And if you have a conflict with another script, a strict script parser should be able to tell if a variable is declared twice and show you where. But, as I say - those types of conflicts can be avoided by not exposing so many global variables.


*for instance, with the page from my last post, if you switch the body to:


<body>
<div id="hours"></div>
<script type="text/javascript">
alert(mydatedata.fldrdate);
</script>
<script type="text/javascript">
alert(typeof hours);
</script>
</body>

Most if not all browsers will still say 'undefined', except for IE which will say 'object' because it's sees it as meaning:


alert(typeof document.all['hours']);

And if you do:


<div id="hours"></div>
<script type="text/javascript">
hours = 12;
alert(typeof hours);
</script>

In IE 8 or less, it will tell you there's an error on the highlighted line - "Object doesn't support this property or method".

That's a fatal error by the way, the kind that stops all subsequent javascript parsing.

All others will be fine and say 'number', which IE 8 and less will do as well if you do it like I was saying:


<div id="hours"></div>
<script type="text/javascript">
var hours = 12;
alert(typeof hours);
</script>

keyboard
02-07-2012, 01:17 AM
Thanks jscheuer1!!!