1

I've been dealing with this annoying bug when creating a new Date(string) in JavaScript. My question is, has anyone found a better/more standardized solution for this issue that works on most browsers? For example I've found that IE9 doesn't like .sss (miliseconds), then Firefox doesn't work correctly with the european date format (DD-MM-YYYY).

Let me know what you think!

Andres Elizondo
  • 331
  • 3
  • 15
  • 2
    Hey! Welcome to Stack Overflow! Stack Overflow is a question and answer site, as such it's expected that questions are to be questions, and answers answers. Could you [edit] your question so that it's only a question, and then add an answer below with the solution? That way the question and the answer can be voted on separately, and google visitors would find it more easily. – Madara's Ghost Dec 13 '16 at 14:12
  • 1
    Why not heed [this answer](http://stackoverflow.com/a/3257513/464709) and stick to date formats the `Date` constructor can understand? As in, guaranteed to understand by the spec? – Frédéric Hamidi Dec 13 '16 at 14:13
  • @FrédéricHamidi because it is not that compatible with old browsers (I had issues with IE9) or would require big coding overhead for such a simple task. – Andres Elizondo Dec 13 '16 at 14:29
  • @MadaraUchiha done :) – Andres Elizondo Dec 13 '16 at 14:31
  • @Andres, I'm pretty sure that RFC 2822 dates (e.g. `October 13, 1975 11:13:00` from the aforementioned answer) are supported by all JavaScript-aware browsers, including IE9. – Frédéric Hamidi Dec 13 '16 at 14:32
  • 1
    Probably useful: http://dygraphs.com/date-formats.html – VLAZ Dec 13 '16 at 14:40
  • @FrédéricHamidi thanks, but that also caused issues with month names (I had to deal with different languages, therefore I needed to use locale settings)... To put it simply, my solution was the one working the best for me, a simple string with no locale settings or extra conditions and very browser compatible. – Andres Elizondo Dec 13 '16 at 14:43
  • @vlaz thanks that's actually very useful!! – Andres Elizondo Dec 13 '16 at 14:46

2 Answers2

2

According to MDN:

Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.

The same applies to Date.parse(). If you fear such inconsistencies (because your browser support is very extensive, or because you handle data from external sources), you can always use the full constructor:

new Date(year, month, [day, hour, minute, second, millisecond])

The parameters between brackets are optional. If you can parse the date using a reliable tool (such as moment.js, as you mentioned), you can then build a native Date with no danger using this.

It's cumbersome, but pack it into a function and never look at it again.

salezica
  • 74,081
  • 25
  • 105
  • 166
1

I have found the string format that works for most browsers is the following.

stringFormat = moment(dateObject).format("YYYY-MM-DDTHH:mm:ss");
date = new Date(stringFormat);

I use moment.js to provide even further compatibility, so far this works well for IE9+, Chrome and Firefox. But took me many several forums and other responses in SOF to find a good solution.

The biggest issue is being compatible with older browsers, locale formats, etc. For example this one was almost correct, but the I found that IE9 doesn't like .sss (miliseconds) and wasn't working either. Firefox had errors with european Date formats too.

Does anyone have a better way to deal with this? Please share the knowledge! :)

Community
  • 1
  • 1
Andres Elizondo
  • 331
  • 3
  • 15