MCPcopy
hub / github.com/date-fns/date-fns / roundToNearestMinutes

Function roundToNearestMinutes

pkgs/core/src/roundToNearestMinutes/index.ts:58–82  ·  view source on GitHub ↗
(
  date: DateArg<DateType>,
  options?: RoundToNearestMinutesOptions<ResultDate>,
)

Source from the content-addressed store, hash-verified

56 * //=> Thu Jul 10 2014 12:30:00
57 */
58export function roundToNearestMinutes<
59 DateType extends Date,
60 ResultDate extends Date = DateType,
61>(
62 date: DateArg<DateType>,
63 options?: RoundToNearestMinutesOptions<ResultDate>,
64): ResultDate {
65 const nearestTo = options?.nearestTo ?? 1;
66
67 if (nearestTo < 1 || nearestTo > 30) return constructFrom(date, NaN);
68
69 const date_ = toDate(date, options?.in);
70 const fractionalSeconds = date_.getSeconds() / 60;
71 const fractionalMilliseconds = date_.getMilliseconds() / 1000 / 60;
72 const minutes =
73 date_.getMinutes() + fractionalSeconds + fractionalMilliseconds;
74
75 const method = options?.roundingMethod ?? "round";
76 const roundingMethod = getRoundingMethod(method);
77
78 const roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo;
79
80 date_.setMinutes(roundedMinutes, 0, 0);
81 return date_;
82}

Callers 1

test.tsFile · 0.90

Calls 3

constructFromFunction · 0.90
toDateFunction · 0.90
getRoundingMethodFunction · 0.90

Tested by

no test coverage detected