UNPKG

1.88 kBTypeScriptView Raw
1import type {
2 NearestMinutes,
3 NearestToUnitOptions,
4 RoundingOptions,
5} from "./types.js";
6/**
7 * The {@link roundToNearestMinutes} function options.
8 */
9export interface RoundToNearestMinutesOptions
10 extends NearestToUnitOptions<NearestMinutes>,
11 RoundingOptions {}
12/**
13 * @name roundToNearestMinutes
14 * @category Minute Helpers
15 * @summary Rounds the given date to the nearest minute
16 *
17 * @description
18 * Rounds the given date to the nearest minute (or number of minutes).
19 * Rounds up when the given date is exactly between the nearest round minutes.
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 minute
27 *
28 * @example
29 * // Round 10 July 2014 12:12:34 to nearest minute:
30 * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
31 * //=> Thu Jul 10 2014 12:13:00
32 *
33 * @example
34 * // Round 10 July 2014 12:12:34 to nearest quarter hour:
35 * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 })
36 * //=> Thu Jul 10 2014 12:15:00
37 *
38 * @example
39 * // Floor (rounds down) 10 July 2014 12:12:34 to nearest minute:
40 * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'floor' })
41 * //=> Thu Jul 10 2014 12:12:00
42 *
43 * @example
44 * // Ceil (rounds up) 10 July 2014 12:12:34 to nearest half hour:
45 * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'ceil', nearestTo: 30 })
46 * //=> Thu Jul 10 2014 12:30:00
47 */
48export declare function roundToNearestMinutes<DateType extends Date>(
49 date: DateType | number | string,
50 options?: RoundToNearestMinutesOptions,
51): DateType;