constructNow.cjs 1.1 KB

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