roundToNearestHours.d.cts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import type {
  2. ContextOptions,
  3. DateArg,
  4. NearestHours,
  5. NearestToUnitOptions,
  6. RoundingOptions,
  7. } from "./types.js";
  8. /**
  9. * The {@link roundToNearestHours} function options.
  10. */
  11. export interface RoundToNearestHoursOptions<DateType extends Date = Date>
  12. extends NearestToUnitOptions<NearestHours>,
  13. RoundingOptions,
  14. ContextOptions<DateType> {}
  15. /**
  16. * @name roundToNearestHours
  17. * @category Hour Helpers
  18. * @summary Rounds the given date to the nearest hour
  19. *
  20. * @description
  21. * Rounds the given date to the nearest hour (or number of hours).
  22. * Rounds up when the given date is exactly between the nearest round hours.
  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 hour
  31. *
  32. * @example
  33. * // Round 10 July 2014 12:34:56 to nearest hour:
  34. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56))
  35. * //=> Thu Jul 10 2014 13:00:00
  36. *
  37. * @example
  38. * // Round 10 July 2014 12:34:56 to nearest half hour:
  39. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 6 })
  40. * //=> Thu Jul 10 2014 12:00:00
  41. *
  42. * @example
  43. * // Round 10 July 2014 12:34:56 to nearest half hour:
  44. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 8 })
  45. * //=> Thu Jul 10 2014 16:00:00
  46. *
  47. * @example
  48. * // Floor (rounds down) 10 July 2014 12:34:56 to nearest hour:
  49. * const result = roundToNearestHours(new Date(2014, 6, 10, 1, 23, 45), { roundingMethod: 'ceil' })
  50. * //=> Thu Jul 10 2014 02:00:00
  51. *
  52. * @example
  53. * // Ceil (rounds up) 10 July 2014 12:34:56 to nearest quarter hour:
  54. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { roundingMethod: 'floor', nearestTo: 8 })
  55. * //=> Thu Jul 10 2014 08:00:00
  56. */
  57. export declare function roundToNearestHours<
  58. DateType extends Date,
  59. ResultDate extends Date = DateType,
  60. >(
  61. date: DateArg<DateType>,
  62. options?: RoundToNearestHoursOptions<ResultDate>,
  63. ): ResultDate;