toDate.cjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. exports.toDate = toDate;
  3. var _index = require("./constructFrom.cjs");
  4. /**
  5. * @name toDate
  6. * @category Common Helpers
  7. * @summary Convert the given argument to an instance of Date.
  8. *
  9. * @description
  10. * Convert the given argument to an instance of Date.
  11. *
  12. * If the argument is an instance of Date, the function returns its clone.
  13. *
  14. * If the argument is a number, it is treated as a timestamp.
  15. *
  16. * If the argument is none of the above, the function returns Invalid Date.
  17. *
  18. * Starting from v3.7.0, it clones a date using `[Symbol.for("constructDateFrom")]`
  19. * enabling to transfer extra properties from the reference date to the new date.
  20. * It's useful for extensions like [`TZDate`](https://github.com/date-fns/tz)
  21. * that accept a time zone as a constructor argument.
  22. *
  23. * **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`.
  24. *
  25. * @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).
  26. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  27. *
  28. * @param argument - The value to convert
  29. *
  30. * @returns The parsed date in the local time zone
  31. *
  32. * @example
  33. * // Clone the date:
  34. * const result = toDate(new Date(2014, 1, 11, 11, 30, 30))
  35. * //=> Tue Feb 11 2014 11:30:30
  36. *
  37. * @example
  38. * // Convert the timestamp to date:
  39. * const result = toDate(1392098430000)
  40. * //=> Tue Feb 11 2014 11:30:30
  41. */
  42. function toDate(argument, context) {
  43. // [TODO] Get rid of `toDate` or `constructFrom`?
  44. return (0, _index.constructFrom)(context || argument, argument);
  45. }