Jump to content

Why can't I change a date object?


boylesg

Recommended Posts

var dateEOW = new Date();dateEOW.setFullYear(2014);dateEOW.setMonth(3);dateEOW.setDate(1);alert(dateEOW);

I give up!

 

Why can't I change the value of a date object with the setDate(...) etc.

 

These functions seem to have no effect and alert(...) simply outputs the current date.

Link to comment
Share on other sites

Actually if I do alert(dateEOF.getDay()) then I get the value I expect, however toDateString() still returns the current system date.

 

Further getDay() function returns the day of the week for the current system date rather than the date that I have set using setDate(...) etc

 

What the ###### is going on with this date object???

 

Is there some other function you have to call after using setDate(...) etc to make the internals of the object consistent??????

Link to comment
Share on other sites

You can set the date with the constructor. I don't see where you're having a problem, though.

var d = new Date(2014, 3, 1);

Be aware that months are numbered from 0 to 11, so 3 would be April.

Link to comment
Share on other sites

Either that or getDay() is simply calculating the wrong value for what ever reason.

 

I am entering 1/3/2014 and according to getDay(), after setDate(1) setMonth(3) setYear(2014), the day of the week is 2 which is simply not correct.

 

The day of the week for April 1 is indeed 2. The method getDay returns 0-6. Similarly getMonth returns 0-11.

 

http://www.w3schools.com/jsref/jsref_getday.asp

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...