constructNow.d.mts 1.2 KB

123456789101112131415161718192021222324252627282930313233
  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. */
  31. export declare function constructNow<DateType extends Date>(
  32. date: DateType | number | string,
  33. ): DateType;