Date Strings from Twitter & Facebook Invaild? (IE & Safari)

Just learned about this yesterday. It seems that if you pass the date string that is returned from Twitter and/or Facebook to the javascript Date() constructor, in IE (and Safari), it shows it as an invalid date.

Twitter returns the “created_time” as something like this: Thu May 01 13:57:04 +0000 2014, which is shown as “Invalid Date” in IE.

//created_time = Thu May 01 13:57:04 +0000 2014
var date = new Date( jsonData.created_time );
console.log( date.toString() );
// = IE 9 & 10: 'Invalid Date' 
// = Chrome:    'Mon May 05 2014 14:50:00 GMT-0400 (Eastern Daylight Time)'
// = FireFox:   'Mon May 05 2014 14:50:00 GMT-0400 (Eastern Standard Time)'
// = Safari:    'Mon May 05 2014 14:50:00 GMT-0400 (Eastern Daylight Time)'

For Facebook, the ‘created_time’ value is something like: 2014-04-17T12:59:04+0000, which is shown as “Invalid Date” in IE and Safari.

//created_time = 2014-04-17T12:59:04+0000
var date = new Date( jsonData.created_time ); 
console.log( date.toString() );
// = IE 9 & 10: 'Invalid Date' 
// = Chrome:    'Thu Apr 17 2014 08:59:04 GMT-0400 (Eastern Daylight Time)'
// = FireFox:   'Thu Apr 17 2014 08:59:04 GMT-0400 (Eastern Standard Time)'
// = Safari:    'Invalid Date'

What fixed the problem, for now, was to do a little manipulation on the date string before it is passed it to the Date() constructor.

//twitter = 'Thu May 01 13:57:04 +0000 2014'
//facebook = '2014-04-17T12:59:04+0000'
    
var created = facebook.created_time;
if( isFacebook ) {
    //this fixes the issue in IE and Safari, and still works in Firefox and Chrome even though they don't need it.
    created = created.replace(/-/g, '/').replace(/T/, ' ').replace(/\+/, ' +');
}
else if( isTwitter ) {
    //this is only an issue in IE, so we can just do a quick test and fix the issue.
    if( navigator.userAgent.match(/MSIE\s([^;]*)/) )
        created = created .replace(/( \+)/, ' UTC$1');
}
var date = new Date( created );