0

I have a date time string September 30, 2017 @ 11:23 am, which is parsable in chrome using:

var end = new Date("September 30, 2017 @ 11:23 am");

But in firefox it gives invalid date error. How do I parse it in Firefox?

Tom O.
  • 5,730
  • 2
  • 21
  • 35
mega6382
  • 9,211
  • 17
  • 48
  • 69
  • Parsing date/time strings is notoriously unreliable because there are so many different formats and most date APIs simply don't bother supporting more than a few standardized formats. You probably need to write your own parsing function or use a library that supports this particular format. – Lennholm Sep 29 '17 at 17:20
  • Are you planning to parse *only this* format? You can use things like regular expression to extract out the `September` part, `30`, `2017` and `11:23 am`. However, if you are trying to parse general date string that may come in many different format you will need a different approach. – maowtm Sep 29 '17 at 17:24

2 Answers2

0

According to MDN

String value representing a date. The string should be in a format recognized by the Date.parse() method

Source

Solution: You can simply remove @ from the stirng. using replace method or any other way.

Ex:

var d = "September 30, 2017 @ 11:23 am"
var end = new Date(d.replace("@",""));

console.log(end)
Mohamed Abbas
  • 2,228
  • 1
  • 11
  • 19
  • If you reference MDN, you should link to the [*relevant part*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date), where it says: "***parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.***". – RobG Sep 30 '17 at 06:57
  • @mega6382 You're welcome. – Mohamed Abbas Sep 30 '17 at 07:58
  • @RobG Thank you. Updated!. – Mohamed Abbas Sep 30 '17 at 07:58
-1

In both Firefox and chrome, removing the @ works. you could replace the @ symbol.

let date = 'September 30, 2017 @ 11:23 am'
new Date(date.replace('@', ''))
Danny Mcwaves
  • 159
  • 1
  • 6