constructNow.d.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import 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. */
  30. export declare function constructNow<
  31. DateType extends Date,
  32. ResultDate extends Date = DateType,
  33. >(date: DateArg<DateType> | ContextFn<ResultDate> | undefined): ResultDate;