DEV Community

Discussion on: Oh Javascript... 🙄

 
kspeakman profile image
Kasey Speakman • Edited

Nope, it does not work. Date.UTC returns unix epoch time (milliseconds since UTC midnight on 1 Jan 1970) as an integer. When you use that to construct a new Date it will still be converted to the local time zone.

// zero-based months 🙄
new Date(Date.UTC(2018, 0 /* Jan */, 1))
<- Sun Dec 31 2017 18:00:00 GMT-0600 (Central Standard Time)

The only reason the linked example prints GMT time zone is because Date.toUTCString() is used. But when using the JS Date object, calculations will be off by a day behind in my local time zone.