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!