1

I have the following date which i convert to a long format. This works fine in chrome but i get an error when i work with firefox. The date object says invalid date. What is wrong with the below code when it just works fine on chrome?

startdate= "2013-08-23 14:59"
alert(startdate);
startdate = new Date(startdate);
alert(startdate);     //Invalid date
startdate = startdate.getTime();
alert(startdate);     //NAN

http://jsfiddle.net/hulk_hulk/kvtW3/

Purnil Soni
  • 831
  • 4
  • 18
Hulk
  • 32,860
  • 62
  • 144
  • 215

5 Answers5

2

The ECMAScript Language Specification describes that Date constructor internally uses Date.parse, which accepts Date Time String Format, which is simplification of the ISO 8601 Extended Format.

"2013-08-23 14:59" doesn't matches with that format, so Firefox fails to parse. But some implementation can parse additional formats, for example Chrome in your case.

Consider using JavaScript date parser/formatter library for browser compatibility. You can easily find millions of libraries from SO/Google.

From the spec

15.9.3.2 new Date (value):

If Type(v) is String, then Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2); let V be the time value for this date.

15.9.4.2 Date.parse (string):

The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

15.9.1.15 Date Time String Format:

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

Community
  • 1
  • 1
Mics
  • 1,420
  • 14
  • 19
2

try this way ..

startdate="2013-08-23 14:59:00"
var a=startdate.split(" ");
var d=a[0].split("-");
var t=a[1].split(":");
startdate= new Date(d[0],(d[1]-1),d[2],t[0],t[1],t[2]);
alert(startdate);     //correct date
startdate = startdate.getTime();
alert(startdate);

hope is useful and Works in IE FF Chrome etc.

Shashank Shukla
  • 1,146
  • 1
  • 17
  • 23
1

Use a library to handle consistent parsing of dates across multiple platforms. Like this one.

https://code.google.com/p/datejs/

Craig
  • 4,268
  • 4
  • 36
  • 53
0

Firefox wants new Date(), try in this way:

    startdate= "2013-08-23 14:59"
    alert(startdate);

    startdate = new Date(2013,08,23,14,59,00,00);

    alert(startdate);     //Invalid date
    startdate = startdate.getTime();
    alert(startdate);     //NAN
djSkAV
  • 3
  • 4
0

The Javascript date support many formats try any of these..!!

Changing the code to this may Help

startdate= "23 09 2013, 14:59:00"
alert(startdate);
startdate = new Date(startdate);
alert(startdate);     //Invalid date
startdate = startdate.getTime();
alert(startdate);  

You can also set values in the following format try in any of this format

date1 = new Date("25 Dec, 1995 23:15:00")
date2 = new Date("2009 06 12,12:52:39")
date3 = new Date("20 09 2006,12:52:39")

look here for JavaScript date details

Dileep
  • 5,362
  • 3
  • 22
  • 38