setMilliseconds.mjs 961 B

123456789101112131415161718192021222324252627282930
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name setMilliseconds
  4. * @category Millisecond Helpers
  5. * @summary Set the milliseconds to the given date.
  6. *
  7. * @description
  8. * Set the milliseconds to the given date.
  9. *
  10. * @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).
  11. *
  12. * @param date - The date to be changed
  13. * @param milliseconds - The milliseconds of the new date
  14. *
  15. * @returns The new date with the milliseconds set
  16. *
  17. * @example
  18. * // Set 300 milliseconds to 1 September 2014 11:30:40.500:
  19. * const result = setMilliseconds(new Date(2014, 8, 1, 11, 30, 40, 500), 300)
  20. * //=> Mon Sep 01 2014 11:30:40.300
  21. */
  22. export function setMilliseconds(date, milliseconds) {
  23. const _date = toDate(date);
  24. _date.setMilliseconds(milliseconds);
  25. return _date;
  26. }
  27. // Fallback for modularized imports:
  28. export default setMilliseconds;