UNPKG

1.15 kBJavaScriptView Raw
1import { constructFrom } from "./constructFrom.js";
2
3/**
4 * @name constructNow
5 * @category Generic Helpers
6 * @summary Constructs a new current date using the passed value constructor.
7 * @pure false
8 *
9 * @description
10 * The function constructs a new current date using the constructor from
11 * the reference date. It helps to build generic functions that accept date
12 * extensions and use the current date.
13 *
14 * It defaults to `Date` if the passed reference date is a number or a string.
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: DateArg<DateType>,
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 function constructNow(date) {
32 return constructFrom(date, Date.now());
33}
34
35// Fallback for modularized imports:
36export default constructNow;