Home >Web Front-end >JS Tutorial >Why Does Date String Conversion Differ Between Chrome and Firefox?

Why Does Date String Conversion Differ Between Chrome and Firefox?

Barbara Streisand
Barbara StreisandOriginal
2024-10-29 10:47:291045browse

Why Does Date String Conversion Differ Between Chrome and Firefox?

Date String Conversion Discrepancy Between Chrome and Firefox

When attempting to convert a date string to a Date object using new Date(), users may encounter different results between Chrome and Firefox. This discrepancy stems from how the two browsers interpret a date string that represents UTC (Coordinated Universal Time).

Firefox vs. Chrome Behavior

In Firefox, the code var date = new Date('2013-02-27T17:00:00'); returns Wed Feb 27 2013 17:00:00 GMT 0700 (SE Asia Standard Time). This is because Firefox assumes the date string is in local time and converts it to the browser's local timezone.

In contrast, Chrome interprets the date string as UTC and returns Thu Feb 28 2013 00:00:00 GMT 0700 (SE Asia Standard Time). This is because the date string is missing the letter "Z" which signifies UTC time.

Correcting the Issue

To ensure that both browsers return the correct UTC date, the date string must be formatted correctly. The correct format for UTC is YYYY-MM-DDTHH:MM:SSZ. In this case, the missing "Z" at the end of '2013-02-27T17:00:00' needs to be appended.

By revising the code to var date = new Date('2013-02-27T17:00:00Z');, both Chrome and Firefox will return the expected UTC date and time, which is Thu Feb 28 2013 00:00:00 GMT 0700 (SE Asia Standard Time).

The above is the detailed content of Why Does Date String Conversion Differ Between Chrome and Firefox?. 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