Home  >  Article  >  Web Front-end  >  Why Does `new Date()` Produce Different Results in Chrome and Firefox When Parsing a Date String?

Why Does `new Date()` Produce Different Results in Chrome and Firefox When Parsing a Date String?

DDD
DDDOriginal
2024-10-26 03:12:02514browse

Why Does `new Date()` Produce Different Results in Chrome and Firefox When Parsing a Date String?

Inconsistent Date Parsing Behavior Between Chrome and Firefox with new Date()

When attempting to convert a date string to a JavaScript Date object, variances arise between Chrome and Firefox. Consider the following code:

var date = new Date('2013-02-27T17:00:00');
console.log(date);

In Firefox, the result is:

Wed Feb 27 2013 17:00:00 GMT+0700 (SE Asia Standard Time)

While in Chrome, the output is:

Thu Feb 28 2013 00:00:00 GMT+0700 (SE Asia Standard Time)

The discrepancy stem from an inconsistent interpretation of the date string's format, which is perceived as UTC in the JSON object received from the server. However, the correct format for UTC is with the 'Z' suffix (Zulu Time), such as '2013-02-27T17:00:00Z'.

To resolve this, you should ensure the date string includes the 'Z' suffix if it does not already. By adhering to the proper UTC format, you can ensure consistent date parsing behavior across different browsers.

The above is the detailed content of Why Does `new Date()` Produce Different Results in Chrome and Firefox When Parsing a Date String?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn