Ishan Jain
Ishan Jain

Reputation: 8161

Why Jquery Date give different value in Chrome?

I have a problem when i worked with Jquery new Date function.

My Jquery code -

alert(new Date('/2013' + " 12:30 am"));
if (!isNaN(new Date('/2013' + " 12:30 am"))) {
    alert('true');
} else {
    alert('false');
}

When i execute this code in Chrome it's always return me true, but on other browser like firefox it's give false.

When alert this jquery new Date('/2013' + " 12:30 am") code -

In Chrome it's give - new Date('/2013' + " 12:30 am") give - Tue Jan 01 2013 00:30:00 GMT+0530 (India Standard Time).

In Firefox it's give - Invalid Date.

Why this code return different value in different browser?

Try Jsfiddle

Upvotes: 2

Views: 274

Answers (1)

Mangiucugna
Mangiucugna

Reputation: 1762

In Firefox when you call new Date(string) the static method Date.parse(string) is called.

But the string format that you used is not supported in Firefox, take a look at the MDN documentation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FDate%2Fparse

I don't know why Chrome accepts this format (I couldn't manage to find the official docs) but it's not cross-browser safe, I would suggest to use new Date (year, month, date, hours, minutes, seconds, ms) that is standard and cross-browser safe

Hope this helps

Upvotes: 1

Related Questions