TimeZoneHelper
Helper for time zone manipulation.
Functions
2
Functions
2Adjusts the time of the specified date to match local system time zone in the specified time zone. i.e. "what time in my timezone would match time in this timezone?"
JavaScript dates are always in the local time zone. This function adjusts the time to match the time in the specified time zone, without altering the time zone. Thus, it won't hold the same time as the original date.
Note that this time zone calculation relies on the browsers built-in functionality to convert a date from a given timezone into a local date by calculating specified time zone UTC offsets and using those to perform the date conversion. If browsers time zone information or interpretation is inaccurate or lacks data, the conversion will probably be inaccurate as well.
const cstDate = new Date(2022, 8, 27, 4); // CST 'America/Chicago'
const localDate = TimeZoneHelper.fromTimeZone(cstDate, 'America/Chicago'); // 2022, 8, 27, 11 (UTC+2 Europe/Stockholm)
| Parameter | Type | Description |
|---|---|---|
date | Date | |
timeZone | String | Number | Timezone supported by Intl.DateFormat or a UTC offset in minutes |
Adjusts the time of the specified date to match the specified time zone. i.e. "what time is it now in this timezone?"
JavaScript dates are always in the local time zone. This function adjusts the time to match the time in the specified time zone, without altering the time zone. Thus, it won't hold the same time as the original date.
Note that this time zone calculation relies on the browsers built-in functionality to convert a local date to a string in a given time zone and then converting the string back into a date. If browsers time zone information or interpretation is inaccurate or lacks data, the conversion will probably be inaccurate as well.
const localDate = new Date(2020, 7, 31, 7); // UTC+2 ('Europe/Stockholm')
const cstDate = TimeZoneHelper.toTimezone(localDate, 'America/Chicago'); // 2020, 7, 31, 0 (still UTC+2, but
// appear as UTC-6)
| Parameter | Type | Description |
|---|---|---|
date | Date | |
timeZone | String | Number | Timezone supported by |