Home > Web Front-end > JS Tutorial > body text

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

DDD
Release: 2024-10-26 03:12:02
Original
512 people have browsed it

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);
Copy after login

In Firefox, the result is:

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

While in Chrome, the output is:

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

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!