UNPKG

1.25 kBTypeScriptView Raw
1/**
2 * @name constructNow
3 * @category Generic Helpers
4 * @summary Constructs a new current date using the passed value constructor.
5 * @pure false
6 *
7 * @description
8 * The function constructs a new current date using the constructor from
9 * the reference date. It helps to build generic functions that accept date
10 * extensions and use the current date.
11 *
12 * It defaults to `Date` if the passed reference date is a number or a string.
13 *
14 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
15 *
16 * @param date - The reference date to take constructor from
17 *
18 * @returns Current date initialized using the given date constructor
19 *
20 * @example
21 * import { constructNow, isSameDay } from 'date-fns'
22 *
23 * function isToday<DateType extends Date>(
24 * date: DateType | number | string,
25 * ): boolean {
26 * // If we were to use `new Date()` directly, the function would behave
27 * // differently in different timezones and return false for the same date.
28 * return isSameDay(date, constructNow(date));
29 * }
30 */
31export declare function constructNow<DateType extends Date>(
32 date: DateType | number | string,
33): DateType;