The Date Math package includes a timezone database and utilities for working with dates in different timezones.
Date object represents a single moment in time that is measured in milliseconds starting on 01 January, 1970 UTC. The
Date objects are always created by using the current browser timezone offset.
The following example demonstrates how to construct a new
Date object for a particular UTC date.
// 22 Oct 2018 12:00:00 GMT
const date = new Date(2018, 9, 22, 15, 0, 0);
The following result in the browser console shows that your current timezone offset is already applied.
> Mon Oct 22 2018 15:00:00 GMT+0300 (EEST)
Date object to JSON, the output is the
'"2014-02-01T10:00:00.000Z"' UTC date that was formatted by using the ISO8601 standard. The JSON serialization methods guarantee that regardless of their particular timezone offsets, the exact moment in time will be represented both in the browser and on the server.
- Before you use the timezone conversion methods, pre-load all timezone details.
Etc/UTCtimezone is loaded by default.
- List of time zones
To import the pre-built timezone scripts, which are generated from the Time Zone Database from the package list, use the following example:
// Load timezone data for a specific time zone
// Load the timezone for a region
// The uncompressed file size is around ~150kB
// Load all available timezone data
// Warning: The uncompressed file size is over 600kB
To create a date in a different timezone and perform calculations, create an instance of the
ZonedDatebehaves similarly to a regular
Date, it is immutable and the
set*methods throw errors.
You can create a date in a different timezone, which represents the same moment in time as a local date by using the
The following example demonstrates how to create a date in a different timezone and output it by using
You can create a date in any time zone by using the
fromUTCDate takes a UTC date, but treats it as a date in the set timezone.
Why UTC dates instead of normal? The Date object will constrain values according to the local time zone Daylight Saving Time rules. This means that not all dates are representable as local dates. Even worse, they'll be silently corrected to the first valid date. This does not apply to the UTC part of the dates, which is making them a safe choice for representing arbitrary dates.
To convert between timezones without creating intermediate local dates, use the
The timezone scripts also provide human readable names for the Internet Assigned Numbers Authority (IANA) timezones and grouped timezone names—for example,
(GMT+02) Helsinki, Sofia....
To list all loaded timezone scripts, use the
To get all IANA timezones that are listed under a specific group, use the