Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript new Date object from String, Different results on IE and FF

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?

like image 761
user1195192 Avatar asked Mar 28 '12 16:03

user1195192


People also ask

Does new Date () return a string?

Use new Date() to get a Date for the current time or Date. now() to get the current time in milliseconds since 01 January, 1970 UTC. Returns a string representation of the current date and time.

How does JavaScript store dates in Adate object?

JavaScript Stores Dates as Milliseconds JavaScript stores dates as number of milliseconds since January 01, 1970, 00:00:00 UTC (Universal Time Coordinated). Zero time is January 01, 1970 00:00:00 UTC.

How does new date work JavaScript?

The Date object is an inbuilt datatype of JavaScript language. It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.

How do I convert a string to a date in TypeScript?

Use the Date() constructor to convert a string to a Date object in TypeScript, e.g. const date = new Date('2024-07-21') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.


2 Answers

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
like image 130
antonjs Avatar answered Sep 20 '22 19:09

antonjs


Try moment.js for all your JS Date woes.

like image 31
dontGoPlastic Avatar answered Sep 22 '22 19:09

dontGoPlastic