@megaorm/zone
Version:
This package is designed to convert UTC datetimes to a specific timezone and helps format and display them as you like.
100 lines (99 loc) • 2.96 kB
TypeScript
/**
* Custom error class for handling errors specific to the Zone class.
*/
export declare class ZoneError extends Error {
}
/**
* Type definition for a formatter function used to format date and time components.
*
* @param year The year component (e.g., 2024).
* @param month The month component (1-12).
* @param day The day of the month (1-31).
* @param hour The hour component in 24-hour format (0-23).
* @param minute The minute component (0-59).
* @param second The second component (0-59).
* @returns A formatted string representation of the date and time.
*/
type Formatter = (year: number, month: number, day: number, hour: number, minute: number, second: number) => string;
/**
* Represents a date and time in a specific timezone, providing utilities
* for formatting and calculating time differences.
*/
export declare class Zone {
/**
* The original date in the local timezone.
*/
date: Date;
/**
* The formatted datetime string in the specified timezone.
*/
datetime: string;
/**
* The timezone identifier (e.g., `America/New_York`)..
*/
timezone: string;
/**
* The year in the specified timezone.
*/
year: number;
/**
* The month (1-12) in the specified timezone.
*/
month: number;
/**
* The day of the month (1-31) in the specified timezone.
*/
day: number;
/**
* The hour (0-23) in the specified timezone.
*/
hour: number;
/**
* The minute (0-59) in the specified timezone.
*/
minute: number;
/**
* The second (0-59) in the specified timezone.
*/
second: number;
/**
* Creates a new Zone instance based on a datetime and timezone.
*
* @param datetime The datetime string in `YYYY-MM-DD hh:mm:ss` fromat.
* @param timezone The timezone identifier (e.g., `America/New_York`).
* @throws `ZoneError` if the datetime or timezone is invalid.
*/
constructor(datetime: string, timezone: string);
/**
* Formats the datetime using a custom formatter function.
*
* @param formatter A function that formats datetime components into a string.
* @returns The formatted date-time string or the default datetime string.
*/
format(formatter: Formatter): string;
/**
* Returns the date as a `YYYY-MM-DD` formatted string.
*
* @returns The date string.
*/
toDateString(): string;
/**
* Returns the time as a `hh:mm:ss` formatted string.
*
* @returns The time string.
*/
toTimestring(): string;
/**
* Returns the time in 12-hour AM/PM format.
*
* @returns The formatted time (e.g., `1:30 PM`).
*/
ampm(): string;
/**
* Calculates the time difference between now and the Zone's date.
*
* @returns A human-readable description of the time difference (e.g., `5 minutes ago`).
*/
diff(): string;
}
export {};