Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

roundToNearestHours

import { roundToNearestHours } from "https://esm.sh/v135/date-fns@3.6.0/index.d.mts";
@example
// Round 10 July 2014 12:34:56 to nearest hour:
const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56))
//=> Thu Jul 10 2014 13:00:00
@example
// Round 10 July 2014 12:34:56 to nearest half hour:
const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 6 })
//=> Thu Jul 10 2014 12:00:00
@example
// Round 10 July 2014 12:34:56 to nearest half hour:
const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 8 })
//=> Thu Jul 10 2014 16:00:00
@example
// Floor (rounds down) 10 July 2014 12:34:56 to nearest hour:
const result = roundToNearestHours(new Date(2014, 6, 10, 1, 23, 45), { roundingMethod: 'ceil' })
//=> Thu Jul 10 2014 02:00:00
@example
// Ceil (rounds up) 10 July 2014 12:34:56 to nearest quarter hour:
const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { roundingMethod: 'floor', nearestTo: 8 })
//=> Thu Jul 10 2014 08:00:00
function roundToNearestHours<DateType extends Date>(date: DateType | number | string, options?: RoundToNearestHoursOptions): Date;
§
roundToNearestHours<DateType extends Date>(date: DateType | number | string, options?: RoundToNearestHoursOptions): Date
[src]

§Type Parameters

§
DateType extends Date
[src]

§Parameters

§
date: DateType | number | string
[src]
  • The date to round
§
  • An object with options.

§Return Type

§
Date
[src]

The new date rounded to the closest hour