Search code examples
unobtrusive-javascript

Why different dates appear the same in JavaScript?


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


Solution

  • 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