2

I have a datetime string that makes its way from an SQL database to my Javascript function.

2013-12-31 09:09:49

Chrome seems to parse it with no issue, but NaN is returned for Firefox and IE.

Why is this happening?

If it makes a difference, I'm using SQLServer 2012.

JoshWillik
  • 2,624
  • 21
  • 38
  • possible duplicate of [Convert MySql DateTime stamp into JavaScript's Date format](http://stackoverflow.com/questions/3075577/convert-mysql-datetime-stamp-into-javascripts-date-format) – Ja͢ck Jan 13 '14 at 16:15

1 Answers1

5

The way SQL creates datestrings (such as with getdate()) seems to differ from the way javascript handles datetime strings.

The Mozilla Devlopment networks says that datetime strings can be in ISO 8601 format.

This format is listed as being 2011-10-10T14:48:00 whereas your SQL string looks like 2011-10-10 14:48:00.

All that needs to be done to make it a JS valid Datetime string is to replace the space with a T.

One way of doing this would be

var dateTime = "2013-12-31 09:09:49";
var jsValidDateTime = dateTime.split(" ").join("T");
Date.parse(jsValidDateTime); //output: 1388498989000

This information is valid as of Firefox 26 and Internet Explorer 11.

JoshWillik
  • 2,624
  • 21
  • 38