0

Safari does not accept the time format I entered. What is the reason?

 this.state.dataDate = "2019-10-15 11:59:27";
 var cacheDateUTC = new Date(this.state.dataDate + "Z");
 var cacheDateLocal = cacheDateUTC.toLocaleString();

 // Chrome browser
 -- "10/15/2019, 3:19:54 PM"

 // Safari browser
 -- "Invalid Date"

I found a solution;

 var cacheDateUTC = moment(this.state.dataDate + "Z").format("MM/DD/YYYY, LTS");
recruintment
  • 65
  • 3
  • 13

2 Answers2

2

Safari has some major differences in the way it treats datetimes.

You can bypass them by just using the moment library.

You can find documentation here: https://momentjs.com/

moment works on safari as well as all other common and current browsers.

Simon Echle
  • 286
  • 2
  • 9
  • I found solution ; var cacheDateUTC = moment(this.state.dataDate + "Z").format("MM/DD/YYYY, LTS"); . Thanks @simon-echle – recruintment Oct 15 '19 at 14:24
  • Using a valid date time string would be much better than adding a large library just for that. – str Oct 18 '19 at 08:27
0

"2019-10-15 11:59:27" is not a format supported by ECMA-262, so parsing is implementation dependent. Safari treats it as a malformed attempt at one of the supported formats as the "T" is missing, so it returns an invalid date.

The supported format requires a "T" between the date and time components, so "2019-10-15T11:59:27" is conforming.

However, that should be treated as local, but Safari treats it as UTC.

The bottom line is to not rely on the built–in parser and always manually parse timestamps (except perhaps for time values, which can be passed directly to the constructor as numbers).

Also see Why does Date.parse give incorrect results?

RobG
  • 142,382
  • 31
  • 172
  • 209