DEV Community

Discussion on: Oh Javascript... 🙄

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

For this particular project, I haven't even written JS code yet. It has all been in Elm. However, Elm's Date.fromString string function calls this JS under the covers: new Date( string ). So it is important to know the behavior. I ran these bits from the browser console.

In this case, example 2 is the one I prefer. Because it is a way to store/transmit a date that will parse to the right day regardless of the current time zone. So hopefully it would not be a linting error.

In an ideal world I would just work with UTC dates, but JS Date can only be made in the local time zone. Working in non-local time zones must be done with something other than JS Date (e.g. unix epoch integer, moment.js, etc).

Collapse
 
guid75 profile image
Guid75

For UTC dates, is developer.mozilla.org/en-US/docs/W... not a possibility?

Thread Thread
 
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.