0

Chrome returns Tue Jun 30 2015 18:00:00 GMT-0700 (Pacific Daylight Time).

Safari returns invalid date for same function.

If ' ' is replaced by 'T' then it works but i don't know why.

new Date('2015-07-01 01:00:00+00:00')Does not work on safari

new Date('2015-07-01T01:00:00+00:00')Works on both browsers

Is there any specific reason?

  • What is with the `momentjs` tag? This is just a vanilla Date object. – VLAZ Sep 12 '19 at 07:25
  • new Date(moment('2015-07-01 01:00:00+00:00')) this works on safari and i want to know why? – Prajesh Mishrikotkar Sep 12 '19 at 07:27
  • Because Moment.js has a lot better date parsing and handling. – VLAZ Sep 12 '19 at 07:28
  • 1
    [`moment(String)`](https://momentjs.com/docs/#/parsing/string/): "When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found." – str Sep 12 '19 at 07:37
  • thanks guys for clarification ... – Prajesh Mishrikotkar Sep 12 '19 at 08:38

1 Answers1

1

The T is part of the ISO Date String format; if it's timezoned it should also have Z (or another timezone indicator) at the end.

The first example is not a standard timestring, but some browsers will attempt to parse it. I guess Safari does not.

For your edit - the moment works in safari because you are using the excellent Moment library to parse it, not the native Safari Date object.

My edit: See VLAZ's comment!

Tobin
  • 1,698
  • 15
  • 24
  • 1
    Minor correction - `T` is *optional* for ISO 8601 strings, however JS `Date` doesn't actually support all of ISO 8601 but a subset and there the `T` is not optional. – VLAZ Sep 12 '19 at 07:29