moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.31k stars 729 forks source link

DateTime.fromISO doesn't work with space, but javascript's Date.parse does #1609

Closed toymachiner62 closed 6 months ago

toymachiner62 commented 6 months ago

Describe the bug A clear and concise description of what the bug is. When a date has a space between the date and time instead of a T, Luxon's DateTime.fromISO() does not parse correctly while the built in javascript Date.parse() does.

I think this is a bug in Luxon and it should properly parse with a space.

DateTime.fromISO('2020-01-01 00:00:00-0800'); // DateTime { Invalid, reason: unparsable }
DateTime.fromISO('2020-01-01T00:00:00-0800'); // DateTime { ts: 2020-01-01T02:00:00.000-06:00, zone: America/Chicago, locale: en-US }
Date.parse('2020-01-01 00:00:00-0800') // 1577865600000
Date.parse('2020-01-01T00:00:00-0800'); // 1577865600000

To Reproduce Please share a minimal code example that triggers the problem:

Actual vs Expected behavior A clear and concise description of what you expected to happen.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

diesieben07 commented 6 months ago

DateTime#fromISO parses only ISO 8601, which requires a T, not a space.

Date.parse is only guaranteed to support a simplified version of ISO 8601, so it also requires a T. Some runtimes might support other formats (like yours with the space), but this is not guaranteed and can change at any time.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse and more specifically https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format

See also: https://github.com/moment/luxon/issues/1325 https://github.com/moment/luxon/issues/990

toymachiner62 commented 6 months ago

The thing that is confusing is that this spec literally says:

NOTE: ISO 8601 defines date and time separated by "T".
      Applications using this syntax may choose, for the sake of
      readability, to specify a full-date and full-time separated by
      (say) a space character.
dasa commented 6 months ago

I raised that as well, but this was Isaac's response: https://github.com/moment/luxon/issues/990#issuecomment-1003686158

diesieben07 commented 6 months ago

That literally says

ISO 8601 defines date and time separated by "T".

There are no ifs or buts about this, if you want valid ISO 8601, you need to use "T". The given grammar above this paragraph also specifies a "T", nothing else (but the grammar also does not claim to be ISO 8601). I don't know what the second sentence of the quote is supposed to refer to, however either way it is about RFC 3339, not ISO 8601.

But all this is really moot, because ISO 8601 is unfortunately not something you can just read for free, as far as I know.