roundToNearestHours.d.ts 2.0 KB

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