5

I am trying to create a new date object from string as follows:

var myDate= new Date("1985-01-01T00:00:00.000-06:00");

On FireFox, it alerts the following

Tue Jan 01 1985 00:00:00 GMT-0600 (Central Standard Time)

On IE8, it alerts the following

NaN

Why IE is acting up this way?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1195192
  • 679
  • 3
  • 11
  • 19

3 Answers3

5

Looking to the documetation the right format is the following:

  new Date(year, month, day [, hour, minute, second, millisecond ]) 

So if you run the following code it will be fine in all browsers:

 var myDate= new Date(1985, 01, 01 , 00, 06, 00, 0000000000);
 myDate // you get the right date in all browsers IE8/7 included
antonjs
  • 14,060
  • 14
  • 65
  • 91
  • this will not get right date,cos ie8 count month from '0' not '1', so 'new Date(1985, 01, 01 , 00, 06, 00, 0000000000)' get Feb not Jun, you need to decrease 1 when you past month parameter – Jack Zhang Apr 10 '14 at 06:37
2

Try moment.js for all your JS Date woes.

dontGoPlastic
  • 1,772
  • 14
  • 22
0

The format is not supported by IE. Maybe you could try using setUTCHours:

var rawdate = new Date("1985/01/01 00:00:00 GMT");
console.log(rawdate);
  //=> in my timezone: Tue Jan 1 01:00:00 UTC+0100 1985
console.log(rawdate.setUTCHours(-6));
  //=> in my timezone that results in: Mon Dec 31 19:00:00 UTC+0100 1984

Or maybe you mean (works in IE, not in other browsers)?

var rawdate = new Date("1985/01/01 00:00:00 GMT-6");
  //=> Tue Jan 1 07:00:00 UTC+0100 1985
KooiInc
  • 119,216
  • 31
  • 141
  • 177