UNPKG

@alessiofrittoli/date-utils

Version:

Lightweight TypeScript date utility functions library

84 lines (82 loc) 3.1 kB
/** * Provides pre-defined constants for common time durations in seconds. * */ declare enum InSeconds { /** One millisecond in seconds. */ _1ms = 0.001, /** One microsecond in seconds. */ _1us = 0.000001, /** One minute in seconds. */ _1M = 60, /** Ten minutes in seconds. */ _10M = 600, /** Tirty minutes in seconds. */ _30M = 1800, /** One hour in seconds. */ _1Hour = 3600, /** One day in seconds. */ _1Day = 86400, /** One week in seconds. */ _1Week = 604800, /** One month in seconds. */ _1Month = 2592000, /** One year in seconds. */ _1Year = 31536000 } /** * Check if date is a valid date. * * @param d The date to check. * @returns True if date is a Date object, false otherwise. */ declare const isValidDate: (d: unknown) => d is Date; /** * A compare function to use as the Array.prototype.sort() method. * * @param date1 The oldest date to compare. * @param date2 The newest date to compare. * @param method ( Optional ) The sorting method. Default: 'DESC'. * - 'ASC' from oldest to newest. * - 'DESC' from newest to oldest. * * @see [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) * * @returns The sorting result. */ declare const sortByDate: (date1: string | number | Date, date2: string | number | Date, method?: "ASC" | "DESC") => number; /** * Get time difference in milliseconds between two dates. * * @param futureDate ( Optional ) The date string | milliseconds since UNIX epoch time | Date object. Default: `new Date()`. * @param olderDate ( Optional ) The older date string | milliseconds since UNIX epoch time | Date object. Default: `new Date()`. * * @returns The difference between the future Date time and the older Date. */ declare const getTimeDiff: (futureDate?: string | number | Date, olderDate?: string | number | Date) => number; /** * Combine Dates. * * By default it will perform an `add` operation. * * You can use a negative number as the Date time value in milliseconds since midnight, January 1, 1970 UTC * to perform a `subtraction` to the reference Date. * * @param a The date string | milliseconds since UNIX epoch time | Date object. Default: `new Date()`. * @param b ( Optional ) The reference date string | milliseconds since UNIX epoch time | Date object. Default: `new Date()`. * * @usage * ```ts * combineDates( 120 * 1000 ) * // will add 2 minutes to the actual local date * combineDates( 120 * 1000, new Date( '01/01/2024 10:00' ) ) * // will add 2 minutes to the given date instance * combineDates( 120 * 1000, '01/01/2024 10:00' ) * // Mon Jan 01 2024 10:02:00 GMT+0100 (Ora standard dell’Europa centrale) * combineDates( -120 * 1000, '01/01/2024 10:00' ) * // Mon Jan 01 2024 09:58:00 GMT+0100 (Ora standard dell’Europa centrale) * ``` * @returns The combined Date. */ declare const combineDates: (a: string | number | Date, b?: string | number | Date) => Date; export { InSeconds, combineDates, getTimeDiff, isValidDate, sortByDate };