UNPKG

@stephenhebert/datetime-iso8601

Version:

A small library to simplify parsing and timezone conversion of computer date formats

41 lines (40 loc) 1.7 kB
import { Iana } from './const'; import type { DateTimeComponents, ParsedTimeComponentKey, ParsedTimeComponents } from './types'; /** * A utility class for parsing, manipulating, and formatting ISO8601 date-time strings. */ declare class DateTimeIso8601 { #private; /** * Constructs a new DateTimeIso8601 instance. * @param isoString An ISO8601 date-time string. * @throws {Error} If the string cannot be parsed. */ constructor(isoString: string); /** * Converts the current date-time to the specified time zone offset. * @param tzOffset The time zone offset as a key of the IANA mapping (e.g., '+00:00'). * @returns The current instance for chaining. * @throws {Error} If the tzOffset is not supported. */ tz(tzOffset: keyof typeof Iana): this; /** * Gets a parsed component or all components of the current date-time. * @param key Optional. The specific component key to retrieve. * @returns The requested component value or an object with all components. */ get<K extends ParsedTimeComponentKey>(key?: K): ParsedTimeComponents | ParsedTimeComponents[K]; /** * Updates the current date-time with new components. * @param newComponents Partial date-time components to update. * @param relative If true, adds values to the current components; otherwise, replaces them. * @returns The current instance for chaining. */ update(newComponents: Partial<DateTimeComponents>, relative?: boolean): this; /** * Returns the ISO8601 string representation of the current date-time. * @returns The ISO8601 string. */ toString(): string; } export { DateTimeIso8601 };