muudless
muudless

Reputation: 7532

Date showing up as NaN in IE?

I know this has been covered a bit online, but I'm still not too sure how to modify this particular piece of code unfortunately:

timeCreated: function(dateString) {     
    var date = new Date(dateString);
    var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

    var hh = date.getHours();
    var m = date.getMinutes();
    var dd = "AM";  
    var h = hh;

    if (h >= 12) {
        h = hh-12;
        dd = "PM";
    }
    if (h == 0) {
        h = 12;
    }

    return h + ':' + date.getMinutes().toString() + ' ' + dd + ' ' + monthNames[date.getMonth()] + ', ' + date.getDate().toString() + ', ' +  date.getFullYear().toString();


}

The dateString is outputting as Tue Nov 06 23:29:33 +0000 2012.

Upvotes: 0

Views: 940

Answers (2)

RobG
RobG

Reputation: 147363

What is the format of dateString?

Built–in support for parsing date strings varies among browsers from poor to awful. Far better to manually parse the string to turn it into a Date object, then go from there. If you provide the string format, further help can be provided.

Upvotes: 0

Yogendra Singh
Yogendra Singh

Reputation: 34367

Your input date format doesn't match any of the standard formats hence IE is unable to parse it i.e. unable to construct the right date object.

Since your Date object is not constructed properly, all methods calls such as date.getHours(); and date.getMinutes(); are returning NaN.

If you use the date string in acceptable format e.g. dateString = "Nov 06 2012 23:29:33 +0000", it works properly.

If you want to support custom format base data parsing, look at http://www.mattkruse.com/javascript/date/, which has a big custom method getDateFromFormat(val,format) to convert any date string in specific format to the Date object. Please have a look and see if that helps.

Upvotes: 2

Related Questions