0

I am getting different dates for the same JavaScript in Safari and Chrome

new Date("2019-11-29T14:00");

returns Fri Nov 29 2019 14:00:00 GMT+0200 (Israel Standard Time) in Chrome but Safari I get: Fri Nov 29 2019 16:00:00 GMT+0200 (Israel Standard Time)

I'm not sure why - what am I missing?

Adrian E
  • 1,884
  • 3
  • 21
  • 33
  • What's the difference between those two values? They look the same to me. – Joachim Sauer Nov 27 '19 at 13:49
  • I see no difference in these... – MadaManu Nov 27 '19 at 13:50
  • 1
    Bad proof-reading. I corrected it and found the reason why. Will share answer now – Adrian E Nov 27 '19 at 13:50
  • 1
    Your input string is not complete; it lacks a time zone indication after the time part. Thus browsers are free to interpret it however they please. If you add a "Z" to the end of the string, for example, you'll force the date/time string to be interpreted as GMT (UST). – Pointy Nov 27 '19 at 13:51
  • @Pointy Thanks! I found it eventually. Wondering if I should remove the question, there already is a post about it in my response – Adrian E Nov 27 '19 at 13:52
  • @Pointy—re "*it lacks a time zone indication … browsers are free to interpret it however they please*" no they aren't. An ISO 8601 format timestamp without a timezone or Z is to be treated as local. Safari is buggy. – RobG Nov 28 '19 at 03:32
  • @RobG well that may be the case; the point is that if you don't have a fully fleshed-out ISO date/time string things may not go so well. – Pointy Nov 28 '19 at 03:47

1 Answers1

1

Turns out I wasn't using the time zone suffix: Safari new Date with string value outs different time

Should be new Date("2019-11-29T14:00Z"); and now the dates are identical

Adrian E
  • 1,884
  • 3
  • 21
  • 33
  • Adding a "Z" changes the timestamp from local to UTC, so not a good solution (unless you wanted UTC, which is not what the timestamp is telling you). Safari's parser is buggy. 2019-11-29T14:00 **should** be treated as local, but Safari treats it as UTC. [Don't use the built–in parser](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). – RobG Nov 27 '19 at 22:00