From 8d05a9a2f1ba51ff1009de003f7d50e0e3757fa0 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 4 Dec 2017 22:10:00 -0600 Subject: [PATCH] Add ISO-8601 Formatted Dates Are Interpreted As UTC as a javascript til --- README.md | 3 +- ...-formatted-dates-are-interpreted-as-utc.md | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 javascript/iso-8601-formatted-dates-are-interpreted-as-utc.md diff --git a/README.md b/README.md index 6d665f4..ac9e822 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_591 TILs and counting..._ +_592 TILs and counting..._ --- @@ -226,6 +226,7 @@ _591 TILs and counting..._ - [Immutable Remove With The Spread Operator](javascript/immutable-remove-with-the-spread-operator.md) - [Initialize A New JavaScript Project With Yarn](javascript/initialize-a-new-javascript-project-with-yarn.md) - [Install The Latest Version Of Node With Nvm](javascript/install-the-latest-version-of-node-with-nvm.md) +- [ISO-8601 Formatted Dates Are Interpreted As UTC](javascript/iso-8601-formatted-dates-are-interpreted-as-utc.md) - [Matching Multiple Values In A Switch Statement](javascript/matching-multiple-values-in-a-switch-statement.md) - [Numbers Are Empty](javascript/numbers-are-empty.md) - [Object Initialization With Shorthand Property Names](javascript/object-initialization-with-shorthand-property-names.md) diff --git a/javascript/iso-8601-formatted-dates-are-interpreted-as-utc.md b/javascript/iso-8601-formatted-dates-are-interpreted-as-utc.md new file mode 100644 index 0000000..eb6a3ea --- /dev/null +++ b/javascript/iso-8601-formatted-dates-are-interpreted-as-utc.md @@ -0,0 +1,28 @@ +# ISO-8601 Formatted Dates Are Interpreted As UTC + +Using `new Date()` or `Date.parse()` with a string that represents a date is +a great way to create a `Date` object for a specified date. A variety of +formats are accepted by these methods. + +But, caution! + +There are subtle differences in how those dates will be interpreted. Given +any old string that reasonably represents a date, the date will be +interpreted using the local time zone, in my case `CST`. + +```javascript +> new Date('2017-12-4') +Mon Dec 04 2017 00:00:00 GMT-0600 (CST) +``` + +However, as soon as we use an ISO-8601 compliant date format, ECMAScript 5 +specifies that the date ought to be interpreted using the UTC time zone. As +you can see, the results are drastic enough to affect what day it comes out +to. + +```javascript +> new Date('2017-12-04') +Sun Dec 03 2017 18:00:00 GMT-0600 (CST) +``` + +[Source](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#ECMAScript_5_ISO-8601_format_support)