UNPKG

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