UNPKG

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