Fun With JavaScript’s Date

new Date(2011, 11, 14).getDate() # => 14

new Date("2011-11-14).getDate() # => 13

Huh? What’s going on here.

While I don’t know for sure, my best guess is that the first form of the constructor simply assigns this.year, this.month, and with the provided values. That’s why the return value is what you’d expect.

The second form, however, seems to be parsing the given date under UTC time and then converting it to local time (EST, in my case). That’s why the second line returns the previous day.

All this hoopla can be gotten around by using Date.getUTCDate when doing simple conversions that don’t care about the timezone.

MDN has more info on Date here.