Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with date formats in JavaScript with different browsers

I am working with dates in an RSS feed, but am finding differing results when using the code below in IE, Chrome and Firefox:

new Date('2001-01-01T12:00:00Z')

Firefox is happy with that, but Chrome and IE return Invalid Date.

I thought I'd try replacing the T and Z as follows:

new Date('2001-01-01 12:00:00')

This time Chrome is happy with that, but Firefox and IE return Invalid Date.

Any ideas what I should do to get a date object in all browsers with this format?!

Many thanks, Tim

like image 610
TimS Avatar asked Aug 25 '10 13:08

TimS


1 Answers

This works in all browsers on my box - try it in the console:

alert(new Date('2001/01/31 12:00:00'))

so

new Date('2001-01-01T12:00:00Z'.replace(/\-/g,'\/').replace(/[T|Z]/g,' '))

IE8, FF3.6, Safari4, Chrome

like image 101
mplungjan Avatar answered Sep 21 '22 00:09

mplungjan