-1

Possible Duplicate:
IE JavaScript date parsing error

This code working fine in chrome and firefox but it didn't work in IE and safari. It return NAN in IE and invalid date in safair.

var date = new Date("2012-10-17T08:15:19.500-05:00");
var now = new Date();
var difference = now - date;

document.write( "Date: " + date.toLocaleString() + "<br/>");
document.write( "Now: " + now.toLocaleString() + "<br/>");
document.write( "Difference: " + differenceToString(difference) );

function differenceToString(milliseconds) {
    var seconds = milliseconds / 1000;
    var numyears = Math.floor(seconds / 31536000);
    var numdays = Math.floor((seconds % 31536000) / 86400);
    var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
    var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
    var numseconds = Math.floor((((seconds % 31536000) % 86400) % 3600) % 60);
    return numyears + " years, " + numdays + " days, " + numhours + " hours, " + numminutes + " minutes, " + numseconds + " seconds";
}

http://jsfiddle.net/RYS3R/

Any idea would be great help.

Thanks

Community
  • 1
  • 1
ravi patel
  • 31
  • 4
  • What part of the code does not work? Is it right the first line? – Bergi Nov 05 '12 at 17:31
  • 1
    I'm marking as a dupe, but be sure to check out [date.js](http://www.datejs.com/) - It's a great library for parsing and working with dates in Javascript! – Mike Christensen Nov 05 '12 at 17:32
  • @bergi : Yes First Line. – ravi patel Nov 05 '12 at 17:32
  • Then please ask only about that, like "Why do IE and Safari not accept my date format while Chrome and FF do?" – Bergi Nov 05 '12 at 17:33
  • The date string formats supported by the Date constructor have only one guarantee: that the it understands the same string format as what it outputs by default. Definitely use a library for dates, since this is a weak area in Javascript functionality. – slashingweapon Nov 05 '12 at 17:33
  • @Mike:It was not duplicate.I do have a different formate of string than the link you provided. – ravi patel Nov 05 '12 at 17:35
  • @ravipatel - The point is, different browsers support different formats. Pick a format that they all have in common and you're good. – Mike Christensen Nov 05 '12 at 17:38
  • @mike: I do understand. But i got the plain text from xml and i can't edit the xml the way i want. It generated from database.So i got "2012-10-17T08:15:19.500-05:00" as a plain text. Thanks – ravi patel Nov 05 '12 at 17:41
  • @ravipatel - Actually it appears not even date.js supports this format. I think you'll have to parse it with RegEx first and extract each part. Bummer. – Mike Christensen Nov 05 '12 at 17:46
  • @ravipatel - Just stumbled across [this library](http://momentjs.com/) too - Might be helpful! – Mike Christensen Nov 05 '12 at 17:50

1 Answers1

0

You forgot the Z before the timezone offset, to get valid JS-parsed date according to the language specification - YYYY-MM-DDTHH:mm:ss.sssZ±hh:mm is the only format it must accept. Try

var date = new Date("2012-10-17T08:15:19.500Z-05:00");
…
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • :: It is not working and as i said i can't edit the string it came from xml so i do not get "Z" in the string. So technically we could not do anything your solution. – ravi patel Nov 05 '12 at 17:47
  • IE does not respect the standard again? Poor thing. And you *can* change the string before passing it into the date constructor, nobody expected you to edit the source. You could do `.replace(/Z?([+-]?\d\d:\d\d)$/, "Z$1")` for example – Bergi Nov 05 '12 at 17:51
  • :: I wil do that too. But your solution still doesn't supported in IE. – ravi patel Nov 05 '12 at 17:53
  • Oh, screw it! [IE supports ISO Date format since version 9](http://msdn.microsoft.com/en-us/library/ie/ff743760(v=vs.94).aspx) - I couldn't find what could be parsed before – Bergi Nov 05 '12 at 18:02