Xinrui Ma
Xinrui Ma

Reputation: 2105

Firefox doesn't work with new Date() with format like "Wed. October 28, 2015"

this is what I have.

 date = testInfoSelect.testDate;
 console.log(date);
 date = new Date(date);
 console.log(date);
 date = $.datepicker.formatDate('mm/dd/yy', date);
 console.log(date);

This is the console output in FF.

"Wed. February 24, 2016"
Invalid Date
"NaN/NaN/NaN"

This is the console output in Chrome.

Wed. February 24, 2016
Wed Feb 24 2016 00:00:00 GMT-0500 (Eastern Standard Time)
02/24/2016

For my application, I can NOT change the date format, which is testInfoSelect.testDate, in this case is "Wed. February 24, 2016".

What I did:

dateString String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).

The most similar date formate in MDN documentation is this:

Date.parse('Wed, 09 Aug 1995 00:00:00 GMT');

What should I do to get the correct result in FF without changing the format of "Wed. February 24, 2016"

Thanks

Upvotes: 1

Views: 278

Answers (1)

user13500
user13500

Reputation: 3856

A hack, as mentioned by @adeno, you could:

new Date("Wed. February 24, 2016".replace('.', ''));

or

new Date("Wed. February 24, 2016".replace('.', ','));

in your case:

new Date(date.replace('.', ''));

To get GMT time you could do something like:

> new Date(date.replace('.', '') + " GMT");
< Date 2016-02-24T00:00:00.000Z

Results for FF, Chrome and Opera:

Firefox:

▶ new Date("Wed. February 24, 2016".replace('.', '') + " GMT");
⯇ Date 2016-02-24T00:00:00.000Z

Chrome:

 > new Date("Wed. February 24, 2016".replace('.', '') + " GMT");
<- Wed Feb 24 2016 01:00:00 GMT+0100 (CET)

Opera:

>>> new Date("Wed. February 24, 2016".replace('.', '') + " GMT");
[+] Wed Feb 24 2016 01:00:00 GMT+0100 

Upvotes: 1

Related Questions