Welcome to WebmasterWorld Guest from 54.196.244.186

Forum Moderators: open

Message Too Old, No Replies

Javascript date returning wrong month

     
11:07 pm on Jun 28, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 1, 2010
posts: 95
votes: 0


I'm sure someone will point out a logical reason for this. I don't understand why the following code is alerting 16th August 2011 and not 16th July?


var x=new Date(2011,07,16);
alert(x);
11:43 pm on June 28, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


Because the month (2nd param) is an integer in the range 0 (January) to 11 (December), not 1 to 12 as you are expecting. So, 7 indicates the 8th month of August.

The day (3rd param) is the usual 1 to 31. But all other params start at 0.
11:57 pm on June 28, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 1, 2010
posts:95
votes: 0


Many thanks penders.