0

A simple new Date() logic behaves differently on Chrome iOS in contrast to other platforms (mac, android, debian etc) - all Chrome packages are recent updates), as the jsfiddle here demonstrates. Why is that and is there a simple fix for this?

My local timezone is Eastern Standard. As I create a Date object with an ISO8601 timestamp string:

If I use

new Date('2018-01-01T10:00:00')
  • chrome 64.0.3282.112 on iOS11.2.2 (an iPhone6+ and an iPadAir2) assumes it's UTC and displays this:

    Mon Jan 01 2018 05:00:00 GMT-0500 (EST)

whereas,

  • MacOSx10.10.3 chrome 64.0.3282.167
  • ubuntu16.04 chrome 64.0.3282.186
  • android8.0.0 chrome 64.0.3282.137, they all assume it's my local timezone and display this:

    Mon Jan 01 2018 10:00:00 GMT-0500 (EST)

If I do specify timezone in the string timestamp

new Date('2018-01-01T10:00:00-05:00')
  • chrome on all four platforms display this:

    Mon Jan 01 2018 10:00:00 GMT-0500 (EST)

wr200m
  • 83
  • 1
  • 10
  • Same answer as always: do not use the built-in parser, it's unreliable. Write a simple parse function for the format you need to parse, or use a library and specify the format when parsing. PS Safari also (erroneously) treats new `Date('2018-01-01T10:00:00')` as UTC. – RobG Feb 26 '18 at 01:41
  • ah ok. after reading the comments above - I now know what to google for and see what you all mean. I guess I'll just write my own parser instead of struggling further. Thanks. – wr200m Feb 26 '18 at 01:45
  • A simple parse function is 2 lines of code: split into number parts, pass to the Date constructor in the right order (remember to subtract 1 from the month). Validating the date (if required) is 2 more lines. ;-) – RobG Feb 26 '18 at 02:34
  • Thank you. Just did that and it works now. Frankly I was using the moment constructor from moment-timezone and I was having this issue before I went back to using Date parse to illustrate my problem here. Perhaps I was not using moment correctly - it was probably wrong to assume moment uses the unreliable Date parser. Anyways, the bespoke parsing works perfectly so I'm giving up the chase. – wr200m Feb 26 '18 at 03:25
  • Moment.js did fallback to using the built-in parser, but that has or is being fixed in recent version, which is a good thing. – RobG Feb 27 '18 at 13:19

0 Answers0