5

I'm using Javascript's Date object to parse a string into a milliseconds timestamp. I'm using Date.parse(), and the strings I'm parsing are of the following format: "2012-07-06 12:59:36-0600"

Date.parse performs nicely in Chrome, parsing into the correct timestamp I'd anticipate. However, every other browser returns "NaN" when I run the string through Date.parse().

I know that the Date object implementation is browser-specific, but I'd like to find a javascript solution that's capable of parsing strings of this type for any browser. Any suggestions on what else I could use in Javascript to achieve this?

dsw88
  • 4,400
  • 8
  • 37
  • 50
  • Split the string in parts, and use the corresponding `Date.prototype.set*` method or the `Date` constructor to get a `Date` instance. Here's a regex to get started: `/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})([-+]\d{4})/`. – Rob W Jul 06 '12 at 19:16

3 Answers3

7

Convert the input to valid ISO 8601:

Date.parse("2012-07-06 12:59:36-0600".replace(' ', 'T'));

This was tested (and works) in Firefox.

Note:

Note that while time zone specifiers are used during date string parsing to properly interpret the argument, they do not affect the value returned, which is always the number of milliseconds between January 1, 1970 00:00:00 UTC and the point in time represented by the argument.

Wayne
  • 59,728
  • 15
  • 131
  • 126
  • Thanks for the suggestion! This made it work in Firefox, but unfortunately didn't work in IE. Big surprise there, eh? :) I think I may just end up going with Date.js even though it's a bit more JS to load. – dsw88 Jul 06 '12 at 19:58
  • @mustang2009cobra - Take a look at this: https://github.com/csnover/js-iso8601/blob/master/iso8601.js – Wayne Jul 06 '12 at 20:35
  • And this: http://stackoverflow.com/questions/5802461/javascript-which-browsers-support-parsing-of-iso-8601-date-string-with-date-par – Wayne Jul 06 '12 at 20:36
  • And this: http://stackoverflow.com/questions/2479714/does-javascript-ecmascript3-support-iso8601-date-parsing – Wayne Jul 06 '12 at 20:36
  • Thanks for the GitHub link! It seems to be exactly what I needed. – dsw88 Jul 13 '12 at 18:01
  • It works in IE9+ without the timezone part... ie, `Date.parse("2012-07-06T12:59:36");` works. – Ray Foss Aug 25 '17 at 15:08
  • This approach is inconsistent across browsers. Firefox will interpret the date as local, while iOS WebView (and probably Safari too) will treat it as UTC. – C-F Mar 08 '18 at 23:08
  • IE 11 timezone parsing works if the timezone includes a ':' between the HH and MM timezone offset, e.g.: `Date.parse("2012-07-06T12:59:36-06:00")` – jv-dev Nov 03 '21 at 18:35
1

Have you tried DateJS? Maybe you don't want to add another library, but it will solve your crossbrowser problem.

davidethell
  • 11,708
  • 6
  • 43
  • 63
  • DateJS seems to be an old library, but it works for what I need here, so I think I'll use it. Any idea if it's still being supported? The most recent version looks like it's from 2007, but I might be wrong on that. – dsw88 Jul 06 '12 at 19:58
  • I don't see any development since 2008 judging from the Google Code repo. Not aware of anything newer that is comparable. – davidethell Jul 06 '12 at 21:02
1

If the format is consistent, you can parse it yourself:

var date = "2012-07-06 12:59:36-0600";
function parseDatetime(input) {
    var match = input.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})([-+]\d{4})/);
    match.shift(); // discard the "full match" index
    match[2]--;
    match[4] += parseInt(match[6],10);
    return new Date(match[0],match[1],match[2],match[3],match[4],match[5]);
}
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592