Comment by tshaddox

Comment by tshaddox 6 months ago

3 replies

> JavaScript date parsing assumes that dates that lack the trailing Z are in the client time zone.

The situation is in fact even worse than this. The current (ECMA262) spec states:

> When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

This is in fact a huge bug in the spec, which was initially unintentional but is now deliberately preserved for web compatibility reasons.

More info here:

https://maggiepint.com/2017/04/11/fixing-javascript-date-web...

stillpointlab 6 months ago

That is good context, thanks! Once I found my bug I had a moment of not being sure who to blame. Honestly, I'm a bit surprised that the ISO8601 spec dictates that absent the Z the date-time ought to be interpreted as local. At the least, I expected there would at least be a way for me to say "trust me JS, this date is UTC so please parse it that way" - but the only way I could find to force that to happen was to manually add a "Z".

But the insanity of inconsistently choosing local/UTC based on the presence of time is genuinely painful. Dates and times are hard enough as it is, why would they do that? It gives me some amusement that this was one of the motivating use cases behind the Temporal spec.

  • tshaddox 6 months ago

    > I'm a bit surprised that the ISO8601 spec dictates that absent the Z the date-time ought to be interpreted as local

    My understanding is that an ISO8601 timestamp with no offset simply means that no time zone is specified, like what the JS Temporal API calls a PlainDateTime. This is a reasonable concept, and to avoid footguns it should be impossible to implicitly convert such a timestamp to a timestamp with a time zone.

    This concept isn’t representable as a JS Date, which will always have a time zone specified (always the local time zone).

  • tshaddox 6 months ago

    > At the least, I expected there would at least be a way for me to say "trust me JS, this date is UTC so please parse it that way" - but the only way I could find to force that to happen was to manually add a "Z".

    FWIW, I always use parseISO from date-fns for this. I agree it’s bonkers that this is necessary!

    https://date-fns.org/v4.1.0/docs/parseISO