PDA

View Full Version : JavaScript Question(s)


PaulSonny
07-02-2008, 07:38 AM
Hello Everybody,

I'm currently trying to learn JavaScript from the w3schools website. I've gone through all the basics and understand pretty well. I've started going through the Js Objects tutorials and i'm currently up to the Date Object, which is where my questions come from.

You can define a Date Object with the new keyword, as shown below.

var myDate=new Date();

The tutorial says that the date object will automatically hold the current date and time as its initial value. Does this mean that the variable myDate always has the latest time, meaning does it update every second, or does it only hold the date/time that it was created and doesn't update?

My other question is about manipulatin dates. It gives you an example like below.

var myDate=new Date();
myDate.setFullYear(2010,0,14);

The date about is 14th January 2010. I'm guessing 0 is January? Is this correct, I assumed it would be 1, being the first month?

If this is correct, does this mean that February would be 1, March would be 2 and so on?

Thats it for now, i'll probably have more questions as i'm working my way through.

Thanks for any help, its much appreciated.

Kind Regards, Paul.

Guest190829
07-02-2008, 07:44 AM
The object will hold the current date and time when it is instantiated, so whenever it is re-instantiated (with a page refresh for example) it will be updated.

And, yes, January starts at 0 and goes up to 11 (December)

PaulSonny
07-02-2008, 07:47 AM
Thanks for the very fast response Danny and clearing up my questions.

Am I right in thinking that if I set a new date as below it would automatically update to March? or would it throw a Js error. I know I wouldn't do this, I just want to understand how it works.

var myDate=new Date();
myDate.setFullYear(2010,1,30);

30th February 2010

Much appreciated, Paul.