Time zone support for date-fns v2.0.0 using the Intl API. By using the browser API no time zone data needs to be included in code bundles. Modern browsers all support the necessary features, and for those that don't a polyfill can be used.
If you do not wish to use a polyfill the time zone option can still be used, but only with time zone offsets such as '-0200' or '+04:00' and not IANA time zone names.
Table of Contents
- Time Zone Helpers
- Time Zone Formatting
- Usage with Node.js
Working with UTC or ISO date strings is easy, and so is working with JS dates when all times are displayed in a user's local time in the browser. The difficulty comes when working with another time zone's local time, other than the current system's, like on a Node server or when showing the time of an event in a specific time zone, like an event in LA at 8pm PST regardless of where a user resides.
In this case there are two relevant pieces of information:
- a fixed moment in time in the form of a timestamp, UTC or ISO date string, and
- the time zone descriptor, usually an offset or IANA time zone name (e.g.
Libraries like Moment and Luxon, which provide their own date time classes, manage these timestamp and time
zone values internally. Sine
date-fns always returns a plain JS Date, which implicitly has the current
system's time zone, helper functions are provided for handling common time zone related use cases.
Time Zone Helpers
To discuss the usage of the time zone helpers let's assume we're writing a system where administrators set up events which will start at a specific time in the venue's local time, and this local time should be shown when accessing the site from anywhere in the world.
Given a date and any time zone, returns a
Date with the equivalent UTC time
Say a user is asked to input the date/time and time zone of an event. A date/time picker will typically return a Date instance with the chosen date, in the user's local time zone, and a select input might provide the actual IANA time zone name.
In order to work with this info effectively it is necessary to find the equivalent UTC time:
const date = // e.g. 2014-06-25 10:00:00 (picked in any time zone)const timeZone = // e.g. America/Los_Angelesconst utcDate = // In June 10am in Los Angeles is 5pm UTC// post 2014-06-25T17:00:00.000Z, America/Los_Angeles
Get a date/time in the local time of any time zone from UTC time
Say the server provided a UTC date/time and a time zone which should be used as initial values for the above form. The date/time picker will take a Date input which will be in the user's local time zone, but the date value must be that of the target time zone.
const isoDate timeZone = // 2014-06-25T10:00:00.000Z, America/New_Yorkconst date = // In June 10am UTC is 6am in New York (-04:00)// 2014-06-25 06:00:00 (in the system time zone)// America/New_York
Time Zone Formatting
format function exported from this library extends
date-fns/format with full time zone support for:
z..zzzUnicode tokens: short specific non-location format
zzzzUnicode token: long specific non-location format
When using those tokens with
date-fns/format it falls back to GMT timezones, and always uses the local
system timezone. For example
zzz in New York would return
GMT-4 instead of the desired
format function will return the latter.
To format a date to a string showing time for a specific time zone, which can be different from the system
time zone, the
format function can be combined with
utcToZonedTime as shown in the example below. To
format function will never change the underlying date, it must be changed to a zoned time
before passing it to
Since a zoned time
Date instance cannot convey the time zone information to the
format function it is
necessary to pass the same
timeZone value as an option on the third argument of
format. When using this
O..OOO tokens will all print the provided time zone rather
than the system time zone.
const date = '2014-10-25T10:46:20Z'const nyTimeZone = 'America/New_York'const parisTimeZone = 'Europe/Paris'const nyDate =const parisDate =// 2014-10-25 06:46:20-04:00// 2014-10-25 06:46:20 EST// 2014-10-25 10:46:20 GMT+2// The time zone name is generated by the Intl API which works best when a locale is also provided// 2014-10-25 10:46:20 CEST// 2014-10-25 10:46:20 Central European Summer Time
toDate function can be used to create a zoned Date from a string containing an offset or IANA
time zone, or by providing the
// Offsets in the date string work as usual and take precedenceconst parisDate =// 2014-10-25 13:46:20+02:00// Since toDate simply clones a Date instance timeZone option is effectively ignored in this caseconst date = '2014-10-25T13:46:20Z'const clonedDate =// When there is no offset in the date string the timeZone property is usedconst bangkokDate =// 2014-10-25 13:46:20+07:00const nyDate =// 2014-10-25 13:46:20-04:00
Note: Since the Intl API does not provide a way to parse long or short time zone names the
function cannot be supported using this approach.
Usage with Node.js
Node.js supports the
Intl API. From v13 Node.js ships with full ICU data included in the binary, however
the current LTS version 12.14 is still built with the
small-icu flag and only contains ICU data for the
en-US locale. To use this library with Node.js 12 and any locale other than
en-US it should be run
ICU data provided at runtime.
The idea of using the Intl API for time zone support was inspired by the Luxon library.
MIT © Marnus Weststrate