timezone-utility
Version:
A versatile timezone management package designed for CommonJS, ES Module (ESM), JavaScript, and TypeScript projects.
136 lines (135 loc) • 5.88 kB
TypeScript
/**
* Timezone Utility
*
* A versatile timezone management package designed for CommonJS, ES Module (ESM), JavaScript, and TypeScript projects.
* It offers a range of features, including timezone listing, retrieving labels and values, region-based filtering, and converting between UTC and other timezones.
* @packageDocumentation
*/
interface TimeZoneEntry {
label: string;
value: string;
country: string;
phoneCode: string;
utcOffset: string;
}
interface ConvertOptions {
is24Hour?: boolean;
dateSeparator?: string;
timeSeparator?: string;
}
type TimeZoneNames = TimeZoneEntry["value"];
/**
* TimeZone class providing static methods for time zone operations.
*/
declare class TimeZone {
static readonly timezones: TimeZoneEntry[];
static readonly regions: string[];
private static timezoneCache;
/**
* Checks if a given string is a valid ISO date-time format.
* @param dateTime - The date-time string to validate.
* @returns True if the string is a valid ISO date-time, false otherwise.
*/
static isISODateTime(dateTime: string): boolean;
/**
* Validates if a given string is a valid time zone.
* @param timeZone - The time zone string to validate.
* @returns True if the time zone is valid, false otherwise.
*/
static isValidTimeZone(timeZone: string): boolean;
/**
* Converts a date-time from one time zone to another.
* @param dateTime - The date-time to convert (Date object or ISO string).
* @param sourceTimeZone - The source time zone.
* @param targetTimeZone - The target time zone.
* @param is24Hour - Whether to use 24-hour format (default: false).
* @returns Converted date-time string or Error if conversion fails.
*/
static convertDateTime(dateTime: Date | string, sourceTimeZone: TimeZoneNames, targetTimeZone: TimeZoneNames, is24Hour?: boolean): string | Error;
/**
* Returns a list of all available time zones.
* @returns Array of TimeZoneEntry objects.
*/
static list(): TimeZoneEntry[];
/**
* Returns a list of all time zone values.
* @returns Array of time zone values.
*/
static listWithOnlyValue(): TimeZoneNames[];
/**
* Returns a list of all time zone labels.
* @returns Array of time zone labels.
*/
static listWithOnlyLabel(): string[];
/**
* Lists time zones for a specific region.
* @param region - The region to filter time zones.
* @returns Array of TimeZoneEntry objects for the specified region.
*/
static listByRegion(region: string): TimeZoneEntry[];
/**
* Lists time zones for a specific country.
* @param country - The country to filter time zones.
* @returns Array of TimeZoneEntry objects for the specified country.
*/
static listByCountry(country: string): TimeZoneEntry[];
/**
* Gets the details for a given time zone value.
* @param value - The time zone value.
* @returns The corresponding details or null if not found.
*/
static getDetailsUsingTimeZoneValue(value: TimeZoneNames): TimeZoneEntry | null;
/**
* Returns a list of all available regions.
* @returns Array of region strings.
*/
static getRegions(): string[];
/**
* Converts a UTC date to a specified time zone.
* @param utcDate - The UTC date (Date object or ISO string).
* @param targetTimeZone - The target time zone.
* @param options - Conversion options (optional).
* @returns Formatted date-time string in the target time zone or error message.
*/
static convertUTCToTimeZone(utcDate: Date | string, targetTimeZone: TimeZoneNames, options?: ConvertOptions): string | null;
/**
* Converts a date-time from a specified time zone to UTC.
* @param dateTime - The date-time to convert (Date object or ISO string).
* @param sourceTimeZone - The source time zone.
* @param options - Conversion options (optional).
* @returns Formatted UTC date-time string or error message.
*/
static convertToUTC(dateTime: Date | string, sourceTimeZone: TimeZoneNames, options?: ConvertOptions): string | Error;
/**
* Converts a date-time between two specified time zones.
* @param date - The date-time string to convert.
* @param fromTimeZone - The source time zone.
* @param toTimeZone - The target time zone.
* @param options - Conversion options (optional).
* @returns Formatted date-time string in the target time zone or error message.
*/
static convertBetweenTimeZones(date: string, fromTimeZone: TimeZoneNames, toTimeZone: TimeZoneNames, options?: ConvertOptions): string | null;
/**
* Gets the current time in a specified time zone.
* @param targetTimeZone - The target time zone.
* @param options - Formatting options (optional).
* @returns Formatted current date-time string in the target time zone or error message.
*/
static getCurrentTimeInTimeZone(targetTimeZone: TimeZoneNames, options?: ConvertOptions): string | null;
/**
* Calculates the time difference between two time zones for a given date.
* @param date - The date string to use for calculation.
* @param fromTimeZone - The source time zone.
* @param toTimeZone - The target time zone.
* @returns Formatted time difference string or error message.
*/
static getTimeDifferenceBetweenTimeZones(date: string, fromTimeZone: TimeZoneNames, toTimeZone: TimeZoneNames): string | null;
/**
* Converts a formatted date-time string to ISO format.
* @param dateTimeString - The date-time string to convert.
* @returns ISO formatted date-time string or null if conversion fails.
*/
static convertToISO(dateTimeString: string): string | null;
}
export { TimeZone };
export default TimeZone;