( date: DateArg<DateType>, options?: RoundToNearestMinutesOptions<ResultDate>, )
| 56 | * //=> Thu Jul 10 2014 12:30:00 |
| 57 | */ |
| 58 | export 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 | } |
no test coverage detected