Does SetMonth (1) give me March?

Why does setMonth (1) give me march? I believe that 0 = Jan, 1 = Feb, 2 = Mar

<!DOCTYPE html> <html> <head> <script> window.onload = init; function init(){ var d = new Date(); d.setFullYear(2014); d.setMonth(1); d.setDate(1); document.getElementById("demo").innerHTML = d; } </script> </head> <body> <div id="demo"></div> </body> </html> 

I get...

Sat Mar 01 2014 15:11:03 GMT-0600 (Central Standard Time)

I am running a 64-bit version of Win7 Pro, and the clock and calendar seem to be correct.

+8
javascript
source share
1 answer

Today is January 31st. When you d.setMonth(1); , you are trying to set the date to February 31st. Since this date does not exist, it falls until March 3rd.

Set the entire date when you initialize the object; do not try to change it piecemeal.

+22
source share

All Articles