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?
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.
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.
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.
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.
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
Try moment.js for all your JS Date woes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With