IE JavaScript date parsing error
Asked Answered
D

6

13

Why cannot IE parse this string as a Date object.

var d = Date.parse("Fri Jun 11 04:55:12 +0000 2010"); // returns NaN

However, it works well in FireFox. I am running IE 8.

Thanks.

Dished answered 11/6, 2010 at 5:58 Comment(1)
This csgpro.com/blog/2016/08/… will be helpful for someoneExtreme
U
10

You are getting NaN value in IE 8 and its working in Firefox because the format of the string varies with browser and operating system.

For example, in IE6 for Windows XP, the string is in the following format:

Tue Dec 05 16:47:20 CDT 2006

But in Firefox for Windows XP, the string is

Tue Dec 05 2006 16:47:20 GMT-0500

to make it compatible with both browser you will have to first check the browser in your javascript code and then accordingly give your input date string.

Utley answered 11/6, 2010 at 6:32 Comment(3)
But is there a compatible common format on all browsers and platforms?Dished
For me, .toJSON() works. It return the date in a format that look like 'yyyy-mm-ddTxx.xx'. I tried in IE8 and FireFox 14.Phosphorate
Hi, Internet explorer not able to parse this date var date = new Date('23-JUL-15'); whereas chrome can parse it. how can I do it in IE?Chlamydeous
A
10

I've found the jQuery Globalization Plugin date parsing to work best. Other methods had cross-browser issues and stuff like date.js had not been updated in quite a while.

You also don't need a datePicker on the page. You can just call something similar to the example given in the docs:

$.datepicker.parseDate('yy-mm-dd', '2007-01-26');
Airwoman answered 30/12, 2010 at 22:3 Comment(0)
H
4

Is solved my problem by creating an date object and let me give it back the timestamp. But for this you need to convert you string into this format:

year, month, date, hours, minutes, seconds,ms

an example would be like:

dateObj = new Date(year, month, date);
timestamp = dateObj.getTime();

This works save in IE and FF.

IE Dev Center: Date Object (JavaScript)

Mozilla Dev Network: Date

For your example you would to something like this:

//your string
var str = "Fri Jun 11 04:55:12 +0000 2010";
//maps months to integer from 0 to 11
var monthArray = {"Jan":0, "Feb":1, "Mar":2, "Apr":3, "May":4, "Jun":5, "Jul":6, "Aug":7, "Sep":8, "Oct":9, "Nev":10, "Dec":11};
//get the values from the string
var regex = /^[^ ]+ ([^ ]+) (\d{1,2}) (\d{2}):(\d{2}):(\d{2}) \+(\d{4}) (\d{4})$/;
match = regex.exec(str);
var month   = monthArray[match[1]],
    date    = match[2],
    hours   = match[3],
    minutes = match[4],
    seconds = match[5],
    ms      = match[6],
    year    = match[7];

//create date object with values
var dateObject = new Date(year, month, date, hours, minutes , seconds, ms);

var ts = dateObject.getTime(); //timestamp in ms
Hebel answered 25/7, 2014 at 6:32 Comment(1)
This! Note jan starts at 0 :)Bluegill
H
1

Problem

In case your date is stored in SQL datetime like 2020-04-07 05:30:00 and want to parse it in IE. When you parse it with JavaScript in IE using new Date(), it outputs Invalid Date while latest versions of Chrome and Firefox parse this date correctly.

Solution

You have to replace <space> with T in datetime string coming from SQL.

Example

let myDate = '2020-04-07 05:30:00';
let myFormattedDate = myDate.replace(' ', 'T'); // '2020-04-07T05:30:00'
console.log(new Date(myFormattedDate));
Henning answered 7/4, 2020 at 16:50 Comment(0)
H
0

because of the +00000. try to add that the last

var d = Date.parse("Fri Jun 11 04:55:12 2010 +0000");
Homolographic answered 11/6, 2010 at 6:15 Comment(0)
V
-1

This may help you. I just solved a problem similar to this.

Problem with Javascript Date function in IE 7, returns NaN

Veta answered 14/7, 2010 at 5:52 Comment(1)
Doesn't matter if it's still helpfulTapia

© 2022 - 2024 — McMap. All rights reserved.