0

I have an element that shows a date string in this format: <div> ListDate: 2014-11-22 00:00:00.0 </div> I have a variable that captures that text, now I am stripping out all but the date string and trying to convert it to a date so I can do some math on it. My code works fine in Chrome, but always returns NaN in FF and Safari. Here is the javascript:

var listDate = " ListDate: 2014-11-22 00:00:00.0 ";
listDate = listDate.replace('ListDate:', '');
listDate = $.trim(listDate);
listDate = Date.parse(listDate);

I'm watching it in Firebug, and it performs as expected up until Date.parse(). I tried the solutions shown on this thread, but still no luck. Any ideas? I don't have control over the html, it comes from a web service.

Community
  • 1
  • 1
Gary D
  • 153
  • 1
  • 10

1 Answers1

1

The full ISO8601 format (IIRC) is expecting a capital letter "T" in between the date and time portions of that string, e.g. ""2014-11-22T00:00:00.0"" https://en.wikipedia.org/wiki/ISO_8601 (I'd therefore recommend always adding the T delimiter.

E.g. the following code will parse to NaN in some browsers:

var myAlmostISODate = "2014-11-22 00:00:00.0";
console.log("Date: " + Date.parse(myAlmostISODate));
//IE11: NaN
//Firefox: 1416632400000
//Chrome: 1416632400000
scunliffe
  • 62,582
  • 25
  • 126
  • 161