0

I have seen that Firefox only successfully parses date objects when fed a string using slashes to delimit month/day/year. SO reference. However, doing this:

var given_input = "2014-09-26 08:00:28.787618";
var d = given_input.replace(/-/g, "/");

var myDate = new Date(d); //Date {Invalid Date}

It works in Chrome. What am I doing wrong?

Community
  • 1
  • 1
1252748
  • 14,597
  • 32
  • 109
  • 229
  • "*Firefox only successfully parses date objects when fed a string using slashes to delimit month/day/year*" -- this is not entirely true; in fact, [the only *required* date format in ECMAScript 5 is `YYYY-MM-DDTHH:mm:ss.sssZ`](http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15), which uses dashes. However, it seems that the use of dashes might require a `T` date/time separator, instead of a space. – apsillers Oct 03 '14 at 15:32
  • Please notice that `1.` your referenced question is over 2 years old and _FireFox_ releases a new version every 6 weeks, i.e. we are now are over 17 versions later. `2.` That question is about `Date.parse` and not `new Date` – Paul S. Oct 03 '14 at 15:37
  • @PaulS. Yes, I see that now. In any case, the problems is the seconds. Apparently only two decimals are allowed. Thanks. – 1252748 Oct 03 '14 at 15:38
  • 1
    @thomas actually, it's a valid date if you put a `"T"` instead of a `" "` between the date and time, without having to do anything to the hyphens – Paul S. Oct 03 '14 at 15:39
  • Can you show an example @PaulS? I don't think I understand what you mean. – 1252748 Oct 03 '14 at 15:40
  • 1
    @thomas `2014-09-26T08:00:28.787618` -- *it's a valid date if you put a `"T"` instead of a `" "`* – apsillers Oct 03 '14 at 15:41
  • @thomas this way of writing a date and time is defined by _ISO 8601_ and calendar dates, (optionally combined with) times and timezone offsets in this format should be understood by any modern browser. – Paul S. Oct 03 '14 at 15:46

1 Answers1

0

The problem, as it turns out is having more than two decimal places at the end of the seconds. It does not follow the format specified by @apsillers. This works if you don't care about time, only date:

input_date.replace(/(\d{4})-(\d{2})-(\d{2}).*$/g, "$1/$2/$3");
1252748
  • 14,597
  • 32
  • 109
  • 229