0

What's the best way to get this to work on both Chrome and Safari? Works fine on Chrome. Returns NaN on Safari.

new Date("2018-11-22 14:24:34 -0800").getTime()
randombits
  • 47,058
  • 76
  • 251
  • 433
  • Possible duplicate of [What are valid Date Time Strings in JavaScript?](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript) – str Nov 22 '18 at 20:38

2 Answers2

1

You shouldn't pass the date as a String to the constructor of Date because that causes Date.parse() to be called.

The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

To mitigate those differences in implementation, I recommend you use a library like moment.js.

connexo
  • 53,704
  • 14
  • 91
  • 128
0

Try using this for the safari browser support:

new Date(("2018-11-22 14:24:34").replace(/-/g, "/")).getTime().valueOf()
Unknown Beginner
  • 173
  • 1
  • 4
  • 13