views:

16

answers:

3
<script type="text/javascript">
alert(new Date(2010,8,31));
alert(new Date(2010,9,1));
</script>

Try the code above. The browser display the same date in both message. Why???

+1  A: 

Did you actually look at the alert? It displays a date in october. The months are zero-based. This means your first line is actually September 31 - which does not exist, and is wrapped to the next day, October 1. Your second line is also October 1.

OregonGhost
A: 

Because javascript months are 0 based, like 0=Jan, 1=Feb

Since September 30 is the last day of the month, javascript corrects it to October 1.

FallenAngel
A: 

Date(2010,8,31) means "October 1, 2010" and Date(2010,9,1) also means "October 1, 2010"

Because

in Date(yyyy,mm,dd), mm can be set from 0 to 11 not from 1 to 12

so that if mm is 8 means august and august have 30 days.

on this case, if you input 31 in dd, it points "August 30" + 1

Hwansoo Kim
Oh, really. Many thanks!
weblap.ro