UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

26 lines (25 loc) 1.54 kB
import type { $Chronos, TimeUnit } from '../types'; declare module '../Chronos' { interface Chronos { /** * @instance Rounds the current date-time to the nearest specified unit and interval. * * - *Rounding is based on proximity to the start or end of the specified unit.* * - *For example, rounding `2025-05-23` by 'day' returns either midnight of May 23 or May 24, depending on the time of day.* * * @param unit - The time unit to round to (`year`, `month`, `week`, `day`, `hour`, `minute`, `second`, `millisecond`). * @param nearest - Optional granularity of rounding. (Defaults to `1`). * * @returns A new `Chronos` instance at the nearest rounded point in time. For wrong unit returns current instance. * * @remarks * - Rounding for `'month'` is based on how far into the month the date is. If past the midpoint, it rounds to the next month. * - Month indices are 0-based internally (January = 0), but the resulting date reflects the correct calendar month. * - For `'week'` unit, rounding is performed by comparing proximity to the start and end of the ISO week (Monday to Sunday). * - If the date is closer to the next Monday, it rounds forward; otherwise, it rounds back to the previous Monday. */ round(unit: TimeUnit, nearest?: number): Chronos; } } /** * Plugin to inject `round` method */ export declare const roundPlugin: ($Chronos: $Chronos) => void;