Reputation: 2507
I've found weird behavior in JavaScript's Date
constructor. It creates times under different time zones for different unix millisecond values. I am in New York, so I expect them all to come out as EST
, but some come out as EDT
instead.
// EST as I expect:
new Date(1446613200000)
Wed Nov 04 2015 00:00:00 GMT-0500 (EST)
new Date(1446440400000)
Mon Nov 02 2015 00:00:00 GMT-0500 (EST)
// EDT for some reason:
new Date(1446354000000)
Sun Nov 01 2015 01:00:00 GMT-0400 (EDT)
new Date(1446181200000)
Fri Oct 30 2015 01:00:00 GMT-0400 (EDT)
I'm able to reproduce this in Chrome and nodejs, but not Firefox. I guess it has to do with v8? Anyone have an explanation for this?
It's a huge shame that JS doesn't provide any sane utility for changing a Date
object's time zone, since it affects the output of getHours
.
Upvotes: 1
Views: 184
Reputation: 1075925
I am in New York, so I expect them all to come out as EST, but some come out as EDT instead.
Yes, the ones that specify a date/time during daylight savings time in your locale come out EDT (Eastern Daylight Time) rather than EST (Eastern Standard Time). The unix epoch values don't have associated timezone, it's just that toString
generates a string based on a "local time" interpretation of it. If you used toISOString
, you'd get a UTC representation for all of them.
Upvotes: 2
Reputation: 665574
Dates don't have a timezone attached in JS. It's just the formatter who does it (use toUTCString()
or getUTCHour()
to get it without). And for your local timezone, the formatter uses summer time when the date is in summer.
Upvotes: 4