Timezone Plugin
Timezone Plugin — Chronos Plugin Documentation.
About Timezone Plugin
Timezone plugin is a plugin that adds timezone-related methods to Chronos.
Import & Usage
Import
To use the Timezone Plugin, import it first:
import { Chronos, chronos } from "chronos-date";
import { timeZonePlugin } from 'chronos-date/plugins/timeZonePlugin';Register
Now you can use the plugin's methods:
// Register the plugin globally
Chronos.use(timeZonePlugin);
// or
chronos.use(timeZonePlugin);
// or
Chronos.register(timeZonePlugin);
// or
chronos.register(timeZonePlugin);Usage
Example usage of the timeZonePlugin:
const c = new Chronos();
const timeZoned = c.timeZone("Asia/Kathmandu");
console.log(timeZoned.getTimeZoneNameShort()); // Get timezone name abbreviation
console.log(timeZoned.getTimeZoneName()); // Get full timezone name
console.log(timeZoned.format()); // Format date in current timezoneExample Usage
Overview
The timeZonePlugin adds comprehensive time zone functionality to Chronos, enabling:
- Time zone conversion and management
- Date-time formatting with current time zone
- Access to time zone names and abbreviations
- Support for different time zone formats (identifier, abbreviation, UTC offset)
Methods Added by the Plugin
Note
All these methods are provided by timeZonePlugin. You must register it using Chronos.use(timeZonePlugin) before calling them. Once registered, all Chronos instances will have access to these methods.
| Method | Description | Returns |
|---|---|---|
timeZone | Creates a new instance of Chronos for the specified time zone. | Chronos |
getTimeZoneName | Returns the current time zone name as a full descriptive string. | string |
getTimeZoneNameShort | Returns the current time zone abbreviation. | string |
API Reference
timeZone()
Creates a new instance of Chronos for the specified time zone identifier, abbreviated time zone name or UTC offset.
Signature
timeZone(tzId: TimeZoneIdentifier): Chronos;
timeZone(zone: TimeZone): Chronos;
timeZone(utc: UTCOffset): Chronos;
timeZone(utc: TimeZoneIdentifier | TimeZone | UTCOffset): Chronos;Parameters
tzId: IANA timezone identifier (e.g.,'Africa/Harare','America/New_York')zone: Standard timezone abbreviation (e.g.,'EST','IST','UTC')utc: UTC offset inUTC±HH:mmformat for fictional/unlisted timezones (e.g.,'UTC+06:15')
Return Type
Chronos - New instance configured with the specified timezone context
Overloads
Timezone Identifier
Creates a new instance using an IANA timezone identifier. This is the recommended approach for accurate timezone handling.
new Chronos('2025-01-15').timeZone('Asia/Dhaka');
new Chronos('2025-01-15').timeZone('America/Los_Angeles');Timezone Abbreviation
Creates a new instance using a standard timezone abbreviation. Use when timezone identifiers are not available.
new Chronos('2025-01-15').timeZone('EST'); // Eastern Standard Time
new Chronos('2025-01-15').timeZone('IST'); // Indian Standard TimeUTC Offset
Creates a new instance using a UTC offset for fictional or unlisted timezones.
new Chronos('2025-01-15').timeZone('UTC+08:00'); // 8 hours ahead of UTC
new Chronos('2025-01-15').timeZone('UTC-05:30'); // 5 hours 30 minutes behind UTCExample
import { Chronos } from 'chronos-date';
import { timeZonePlugin } from 'chronos-date/plugins/timeZonePlugin';
// Register plugin
Chronos.use(timeZonePlugin);
// Using IANA identifier (recommended)
const dhakaTime = new Chronos('2025-01-15').timeZone('Asia/Dhaka');
// Using timezone abbreviation
const easternTime = new Chronos('2025-01-15').timeZone('EST');
// Using UTC offset
const customOffset = new Chronos('2025-01-15').timeZone('UTC+06:30');Remarks
- Timezone identifiers provide the most accurate timezone handling and are recommended for production use
- Timezone abbreviations should be used only when identifiers are not available, as they may have ambiguous interpretations
- UTC offsets are suitable for fictional timezones or regions not covered by the IANA database
- Invalid input automatically falls back to
UTCtimezone - Returns a new
Chronosinstance - the original instance remains unchanged
getTimeZoneName()
Get full time zone name or UTC offset
Signature
getTimeZoneName(utc?: UTCOffset): LooseLiteral<TimeZoneName | UTCOffset>Parameters
utc?— (Optional) A UTC offset string in the format"UTC+06:00","UTC-04:30", etc.- If provided, it bypasses the instance's current offset and returns the name mapped to this offset instead.
Return Value
LooseLiteral<UTCOffset>— The current time zone name as a full descriptivestring(e.g."Bangladesh Standard Time") or the fallback UTC offset (UTCOffset).
Example Usage
Info
- This method uses a predefined mapping of UTC offsets to time zone names.
- If multiple time zones share the same UTC offset, it returns the first match from the predefined list.
- If no match is found (which is rare), it falls back to returning the UTC offset (e.g.
"UTC+06:15"). - Passing a custom
utcoffset overrides system/instance's offset detection. - To retrieve the local system's native time zone name (or its identifier if the name is unavailable), use the $getNativeTimeZoneName instance method.
- To retrieve the local system's native time zone identifier, use the $getNativeTimeZoneId instance method.
getTimeZoneNameShort()
Get abbreviated time zone name or UTC offset
Signature
getTimeZoneNameShort(utc?: UTCOffset): TimeZone | UTCOffsetParameters
utc?— (Optional) A UTC offset string in the format"UTC+06:00","UTC-04:30", etc.- If provided, it bypasses the instance's current offset and returns the name mapped to this offset instead.
Return Value
TimeZone | UTCOffset— The current time zone abbreviation (e.g."BST"forBangladesh Standard Time).
Example Usage
Info
- This method uses a predefined mapping of UTC offsets to time zone names.
- If multiple time zones share the same UTC offset, it returns the first match from the predefined list.
- If no match is found (which is rare), it falls back to returning the UTC offset (e.g.
"UTC+06:15"). - Passing a custom
utcoffset overrides system/instance's offset detection. - To retrieve the local system's native time zone name (or its identifier if the name is unavailable), use the $getNativeTimeZoneName instance method.
- To retrieve the local system's native time zone identifier, use the $getNativeTimeZoneId instance method.
ALias
Also available as getTimeZoneNameAbbr() alias.
Last updated: Wed, Jun 24, 2026 09:04:13AM (UTC)
