setSeconds.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { toDate } from "./toDate.js";
  2. /**
  3. * The {@link setSeconds} function options.
  4. */
  5. /**
  6. * @name setSeconds
  7. * @category Second Helpers
  8. * @summary Set the seconds to the given date, with context support.
  9. *
  10. * @description
  11. * Set the seconds to the given date, with an optional context for time zone specification.
  12. *
  13. * @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).
  14. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  15. *
  16. * @param date - The date to be changed
  17. * @param seconds - The seconds of the new date
  18. * @param options - An object with options
  19. *
  20. * @returns The new date with the seconds set
  21. *
  22. * @example
  23. * // Set 45 seconds to 1 September 2014 11:30:40:
  24. * const result = setSeconds(new Date(2014, 8, 1, 11, 30, 40), 45)
  25. * //=> Mon Sep 01 2014 11:30:45
  26. */
  27. export function setSeconds(date, seconds, options) {
  28. const _date = toDate(date, options?.in);
  29. _date.setSeconds(seconds);
  30. return _date;
  31. }
  32. // Fallback for modularized imports:
  33. export default setSeconds;