1

How can I parse string "1890-09-30T23:59:59+01:16:20" in JavaScript?

The string returned by Java server offsetDateTime.format(ISO_OFFSET_DATE_TIME)

Test: new Date('1890-09-30T23:59:59+01:16:20') => Invalid Date

More info about this strange datetime: https://www.timeanddate.com/time/change/hungary/budapest?year=1890

Note: Angular 2's date pipe fails too.

Zoltán
  • 61
  • 4
  • new Date() fails for the local time reference of `+01:16:20` at the end. If you slice off the end, parse `1890-09-30T23:59:59` and then add the `01:16:20` to it again, you'll get the correct date. – Shilly Feb 22 '17 at 16:31
  • That is a weird offset. What time zone does it represent? There's an answer to [*Cross browser and future proof method of extracting date from ISO 8601 format*](http://stackoverflow.com/questions/37662898/cross-browser-and-future-proof-method-of-extracting-date-from-iso-8601-format/37670025#37670025) to parse ISO format strings with an offset, though it only expects the offset in minutes. It would be simple to modify it to accommodate an offset with seconds. – RobG Feb 23 '17 at 00:41
  • I don't agree that the "duplicate" is a duplicate. The answers aren't focussed on parsing and don't address the OP's issue. – RobG Feb 23 '17 at 03:38

1 Answers1

0

Here's one idea:

'1890-09-30T23:59:59+01:16:20'.split(/[-,:,T,+]+/);

This will return an array of your numerical values.

[ '1890', '09', '30', '23', '59', '59', '01', '16', '20' ]

  • The parsed result should be equal with new Date("1890-09-30T23:43:39+01:00") or new Date("1890-09-30T22:43:39Z") – Zoltán Feb 22 '17 at 16:59