roundToNearestMinutes.d.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import type {
  2. ContextOptions,
  3. DateArg,
  4. NearestMinutes,
  5. NearestToUnitOptions,
  6. RoundingOptions,
  7. } from "./types.js";
  8. /**
  9. * The {@link roundToNearestMinutes} function options.
  10. */
  11. export interface RoundToNearestMinutesOptions<DateType extends Date = Date>
  12. extends NearestToUnitOptions<NearestMinutes>,
  13. RoundingOptions,
  14. ContextOptions<DateType> {}
  15. /**
  16. * @name roundToNearestMinutes
  17. * @category Minute Helpers
  18. * @summary Rounds the given date to the nearest minute
  19. *
  20. * @description
  21. * Rounds the given date to the nearest minute (or number of minutes).
  22. * Rounds up when the given date is exactly between the nearest round minutes.
  23. *
  24. * @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).
  25. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  26. *
  27. * @param date - The date to round
  28. * @param options - An object with options.
  29. *
  30. * @returns The new date rounded to the closest minute
  31. *
  32. * @example
  33. * // Round 10 July 2014 12:12:34 to nearest minute:
  34. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
  35. * //=> Thu Jul 10 2014 12:13:00
  36. *
  37. * @example
  38. * // Round 10 July 2014 12:12:34 to nearest quarter hour:
  39. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 })
  40. * //=> Thu Jul 10 2014 12:15:00
  41. *
  42. * @example
  43. * // Floor (rounds down) 10 July 2014 12:12:34 to nearest minute:
  44. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'floor' })
  45. * //=> Thu Jul 10 2014 12:12:00
  46. *
  47. * @example
  48. * // Ceil (rounds up) 10 July 2014 12:12:34 to nearest half hour:
  49. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'ceil', nearestTo: 30 })
  50. * //=> Thu Jul 10 2014 12:30:00
  51. */
  52. export declare function roundToNearestMinutes<
  53. DateType extends Date,
  54. ResultDate extends Date = DateType,
  55. >(
  56. date: DateArg<DateType>,
  57. options?: RoundToNearestMinutesOptions<ResultDate>,
  58. ): ResultDate;