daykit
Version:
A lightweight, type-safe date manipulation library for TypeScript/JavaScript with comprehensive timezone support
150 lines (148 loc) • 5.25 kB
TypeScript
/**
* Supported time units for date manipulation
*/
type TimeUnit = "days" | "hours" | "minutes" | "seconds" | "milliseconds";
/**
* Supported format tokens for date formatting
*/
type FormatToken = "YYYY" | "YY" | "MMMM" | "MMM" | "MM" | "DD" | "ddd" | "dddd" | "HH" | "hh" | "mm" | "ss" | "A" | "a";
/**
* Supported locale options
*/
type Locale = string;
/**
* Supported timezone options
*/
type TimeZone = string;
/**
* Timezone information
*/
interface TimezoneInfo {
name: string;
offset: number;
isDST: boolean;
abbreviation: string;
}
/**
* Date formatting options
*/
interface FormatOptions {
locale?: Locale;
timeZone?: TimeZone;
hour12?: boolean;
}
/**
* Creates a new Date object from the input date
* @param date - Input date (defaults to current date)
* @returns A new Date object
*/
declare function createMoment(date?: Date | string | number): Date;
/**
* Gets a list of available timezones
* @returns Array of timezone strings
*/
declare function getAvailableTimezones(): string[];
/**
* Gets timezone information for a specific date and timezone
* @param date - The date to get timezone info for
* @param timeZone - The timezone to get info for
* @returns Timezone information object
*/
declare function getTimezoneInfo(date: Date | string | number, timeZone: TimeZone): TimezoneInfo;
/**
* Checks if a timezone is currently in DST
* @param timeZone - The timezone to check
* @returns True if the timezone is in DST
*/
declare function isDST(timeZone: TimeZone): boolean;
/**
* Gets the DST transition dates for a timezone in a given year
* @param timeZone - The timezone to check
* @param year - The year to check (defaults to current year)
* @returns Object containing start and end dates of DST
*/
declare function getDSTTransitions(timeZone: TimeZone, year?: number): {
start: Date | null;
end: Date | null;
};
/**
* Formats a date according to the specified format string
* @param date - The date to format
* @param fmt - Format string (default: "YYYY-MM-DD HH:mm:ss")
* @param options - Formatting options
* @returns Formatted date string
*/
declare function format(date: Date | string | number, fmt?: string, options?: FormatOptions): string;
/**
* Converts a date to a specific timezone
* @param date - The date to convert
* @param timeZone - Target timezone
* @returns New date in the target timezone
*/
declare function toTimezone(date: Date | string | number, timeZone: TimeZone): Date;
/**
* Gets the timezone offset in minutes
* @param date - The date to get offset for
* @param timeZone - Target timezone
* @returns Offset in minutes
*/
declare function getTimezoneOffset(date: Date | string | number, timeZone: TimeZone): number;
/**
* Gets a list of available locales
* @returns Array of locale strings
*/
declare function getAvailableLocales(): string[];
/**
* Adds time to a date
* @param date - The date to add time to
* @param n - Number of units to add
* @param unit - Time unit to add
* @returns New date with time added
*/
declare function add(date: Date | string | number, n: number, unit: TimeUnit): Date;
/**
* Subtracts time from a date
* @param date - The date to subtract time from
* @param n - Number of units to subtract
* @param unit - Time unit to subtract
* @returns New date with time subtracted
*/
declare function subtract(date: Date | string | number, n: number, unit: TimeUnit): Date;
/**
* Calculates the difference between two dates
* @param date1 - First date
* @param date2 - Second date
* @param unit - Time unit for the difference (default: "milliseconds")
* @returns Difference in the specified unit
*/
declare function diff(date1: Date | string | number, date2: Date | string | number, unit?: TimeUnit): number;
/**
* Checks if date1 is before date2
* @param date1 - First date
* @param date2 - Second date
* @returns True if date1 is before date2
*/
declare function isBefore(date1: Date | string | number, date2: Date | string | number): boolean;
/**
* Checks if date1 is after date2
* @param date1 - First date
* @param date2 - Second date
* @returns True if date1 is after date2
*/
declare function isAfter(date1: Date | string | number, date2: Date | string | number): boolean;
/**
* Returns the start of the specified unit
* @param date - The date to get the start of
* @param unit - Time unit to get the start of
* @returns New date at the start of the specified unit
*/
declare function startOf(date: Date | string | number, unit: "day" | "hour" | "minute"): Date;
/**
* Returns a human-readable string representing the time difference
* @param date - The date to compare
* @param now - The reference date (defaults to current date)
* @param options - Formatting options
* @returns Human-readable time difference string
*/
declare function fromNow(date: Date | string | number, now?: Date | string | number, options?: FormatOptions): string;
export { type FormatOptions, type FormatToken, type Locale, type TimeUnit, type TimeZone, type TimezoneInfo, add, createMoment, diff, format, fromNow, getAvailableLocales, getAvailableTimezones, getDSTTransitions, getTimezoneInfo, getTimezoneOffset, isAfter, isBefore, isDST, startOf, subtract, toTimezone };