UNPKG

@etsoo/shared

Version:

TypeScript shared utilities and functions

97 lines (96 loc) 2.89 kB
declare global { interface Date { /** * Substract date * @param input Input date */ substract(input: Date): TimeSpan; } } /** * TimeSpan */ export interface TimeSpan { totalMiniseconds: number; totalSeconds: number; totalMinutes: number; totalHours: number; totalDays: number; totalMonths: number; totalYears: number; } type DateType = Date | string | null | undefined; type DateReturn<T extends DateType, R> = T extends Date ? R : R | undefined; export declare namespace DateUtils { /** * Day format, YYYY-MM-DD */ const DayFormat: Intl.DateTimeFormatOptions; /** * Minute format, YYYY-MM-DD hh:mm */ const MinuteFormat: Intl.DateTimeFormatOptions; /** * Second format, YYYY-MM-DD hh:mm:ss */ const SecondFormat: Intl.DateTimeFormatOptions; /** * Date format options */ type FormatOptions = Intl.DateTimeFormatOptions | "d" | "dm" | "ds"; /** * Format * @param input Input date time * @param locale Locale * @param options Options * @param timeZone Time zone */ function format<T extends DateType>(input: T, locale?: string | string[], options?: FormatOptions, timeZone?: string): DateReturn<T, string>; /** * Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property * @param date Input date * @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type */ function formatForInput<T extends DateType>(date: T, hasSecondOrType?: boolean | string): DateReturn<T, string>; /** * Get month's days * @param year Year * @param month Month, starts from 0 * @returns Days */ const getDays: (year: number, month: number) => number; /** * Is the test date expired to now * @param testDate Test date * @returns Result */ function isExpired(testDate?: Date | string | null): boolean; /** * Build JSON parser * @param keys Date field names * @returns JSON parser */ const jsonParser: (keys: string[]) => (key: string, value: unknown) => unknown; /** * Parse string to date * 2021/10/31 or 2021-10-31 * @param input Input string * @returns Date */ function parse<T extends DateType>(input: T): DateReturn<T, Date>; /** * Two dates are in the same day * @param d1 First date * @param d2 Second date * @returns Result */ function sameDay(d1?: Date | string | null, d2?: Date | string | null): boolean; /** * Two dates are in the same month * @param d1 First date * @param d2 Second date * @returns Result */ function sameMonth(d1?: Date | string | null, d2?: Date | string | null): boolean; } export {};