Skip to content

Commit

Permalink
Updated Date.parse() (#32253)
Browse files Browse the repository at this point in the history
* updated the non-standard date strings

* added a note about DST with Date.parse()

* Update files/en-us/web/javascript/reference/global_objects/date/parse/index.md

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>

* moved the note on different DST settings to getTimezoneOffset

---------

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
  • Loading branch information
dletorey and Josh-Cena committed Feb 20, 2024
1 parent 60e5d26 commit bcad817
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Expand Up @@ -45,6 +45,8 @@ In a region that annually shifts in and out of Daylight Saving Time (DST), as `d

> **Note:** `getTimezoneOffset()`'s behavior will never differ based on the time when the code is run — its behavior is always consistent when running in the same region. Only the value of `date` affects the result.
> **Note:** [Many countries have experimented with not changing the time twice a year](https://en.wikipedia.org/wiki/Daylight_saving_time_by_country#Past_observance) and this has meant that DST has continued over the winter too. For example in the UK DST lasted from 2:00AM 18 February 1968 to 3:00AM 31 October 1971, so during the winter the clocks were not set back.
In most implementations, the [IANA time zone database](https://en.wikipedia.org/wiki/Daylight_saving_time#IANA_time_zone_database) (tzdata) is used to precisely determine the offset of the local timezone at the moment of the `date`. However, if such information is unavailable, an implementation may return zero.

## Examples
Expand Down
Expand Up @@ -58,6 +58,8 @@ Date.parse("2019-01-01T00:00:00");
Implementations usually default to the local time zone when the date string is non-standard. For consistency, we will assume that the code uses the UTC timezone.

> **Note:** The local time zone offset comes from the system setting of the device and is then applied to the date being parsed. [Daylight Saving Time (DST), of the local time zone, can also have an effect on this too](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset#varied_results_in_daylight_saving_time_dst_regions).
```js
Date.parse("Jan 1, 1970"); // 0 in all implementations

Expand All @@ -75,12 +77,14 @@ Date.parse("Thu, 01 Jan 1970 00:00:00 GMT+0300");

// Single number
Date.parse("0");
// 946684800000 in Chrome (Sat Jan 01 2000 00:00:00 GMT+0000);
// NaN in Firefox;
// NaN in Firefox ≤122
// 946684800000 in Chrome and Firefox ≥123 (Sat Jan 01 2000 00:00:00 GMT+0000);
// -62167219200000 in Safari (Sat Jan 01 0000 00:00:00 GMT+0000)

// Two-digit number that may be a month
Date.parse("28"); // NaN in all implementations
Date.parse("28");
// NaN Chrome and Firefox
// -61283606400000 in Safari (Fri Dec 31 0027 23:58:45 GMT-0001)

// Two-digit year
Date.parse("70/01/01"); // 0 in all implementations
Expand All @@ -91,8 +95,8 @@ Date.parse("Mar 32, 2014"); // NaN in all implementations
Date.parse("2014/25/23"); // NaN in all implementations

Date.parse("2014-02-30");
// NaN in Safari and Firefox;
// 1393718400000 in Chrome (Sun Mar 02 2014 00:00:00 GMT+0000)
// NaN in Safari
// 1393718400000 in Chrome and Firefox (Sun Mar 02 2014 00:00:00 GMT+0000)
Date.parse("02/30/2014"); // 1393718400000 in all implementations

// Chrome, Safari, and Firefox 122 and later parse only the first three letters for the month.
Expand Down

0 comments on commit bcad817

Please sign in to comment.