1

I have a timestamp in the following format:

[yyyy-MM-ddThh:mm:ssZ] (example: 2015-05-15T03:34:17Z)

I want's to parse this timestamp into Date and format is like: [Fri May 15 2015 09:04:17 GMT +5:30]:

Now, i am use the following code to parse and it work's fine in Firefox 3.6+ browsers. But the problem is, it's not working in internet explorer (IE) 8, in IE it returns 'NaN'.

My Javascript code is:

var myDate = new Date(timestampDate); 
//In Firefox i get myDate as Date object and i can get day, month and year. But in IE this myDate value is coming as NaN

var day = myDate.getDate();
var month = myDate.getMonth();
var year = myDate.getFullYear();

Any help is rewarded. Please give me a solution to make this working in IE also.

OammieR
  • 2,800
  • 5
  • 30
  • 51
lingadurai
  • 13
  • 4
  • where does the `timestampDate` come from and what is its value ? What statement return `Nan` ? – pomeh Apr 24 '12 at 09:06
  • 1
    Have a look at http://stackoverflow.com/questions/1088793/the-correct-javascript-date-parse-format-string – Andreas Apr 24 '12 at 09:09
  • 1
    possible duplicate of [javascript dates in IE, NAN - firefox & chrome ok](http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok) – Jon Apr 24 '12 at 09:10
  • also this could help http://stackoverflow.com/questions/3020508/ie-javascript-date-parsing-error – pomeh Apr 24 '12 at 09:10

3 Answers3

1

yyyy-MM-ddThh:mm:ssZ is an ISO date. Browsers don't support them very well, FireFox doesn't parse 2015-05-15T03:34:17+01 for example.

You'll have to extract the elements out of the string manually before creating your date.

Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46
  • @kennebec: Oops, I mixed basic and extended format. Regardless, the `:` is optional, and if you dont provide it, it doesn't work in Firefox (it's only necessary if the time-zone is not a round number of hours). Both are allowed in Chrome, neither in Safari 5, IE8. – Lee Kowalkowski Apr 24 '12 at 13:38
1
(function(){
    //if the browser correctly parses the test string, use its native method.
    var D= new Date('2011-06-02T09:34:29+02:00');

    if(D && +D=== 1307000069000) Date.fromISO= function(s){
        return new Date(s);
    };
     Date.fromISO= function(s){
        var day, tz,
        rx=/^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/,
        p= rx.exec(s) || [];
        if(p[1]){
            //extract the y-m-d h:m:s.ms digits:
            day= p[1].split(/\D/);
            for(var i= 0, L= day.length; i<L; i++){
                day[i]= parseInt(day[i], 10) || 0;
            };
            day[1]-= 1; //adjust month
            //create the GMT date:
            day= new Date(Date.UTC.apply(Date, day));
            if(!day.getDate()) return NaN;
            if(p[5]){
                // adjust for the timezone, if any:
                tz= (parseInt(p[5], 10)*60);
                if(p[6]) tz+= parseInt(p[6], 10);
                if(p[4]== '+') tz*= -1;
                if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
            }
            return day;
        }
        return NaN;
    }
})();

//test alert(Date.fromISO("2015-05-15T03:34:17Z").toUTCString())

kennebec
  • 102,654
  • 32
  • 106
  • 127
0

I had the same problem and found this, supposing that you use jQuery UI:

$.datepicker.parseDate('yy-mm-dd', '2014-02-14');

This is a useful method of the UI Datepicker. I'm just about to write my own date parser to make my code jqui-independent but it may help others so I leave this answer here.

dkellner
  • 8,726
  • 2
  • 49
  • 47