@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
27 lines (26 loc) • 1.43 kB
TypeScript
import { Temporal } from "@js-temporal/polyfill";
import type { DateTimeUnit } from "../../types/index.js";
/**
* Round an ISO 8601 datetime string to the specified date-time unit.
*
* - Returns "" for invalid inputs.
* - Accepts all date and time units: "year", "month", "week", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond".
* - Time units use Temporal.PlainDateTime.round() directly.
* - Date units (year, month, week) use manual start-of-unit rounding.
* - Wraps all Temporal calls in try-catch; returns "" on any error.
*
* @param value ISO 8601 datetime string
* @param options Rounding options: smallestUnit, optional roundingIncrement and roundingMode
* @returns Rounded ISO 8601 datetime string, or "" on invalid input
*
* @example roundDateTime("2024-06-15T12:34:56", { smallestUnit: "year" }) // "2024-01-01T00:00:00"
* @example roundDateTime("2024-06-15T12:34:56", { smallestUnit: "month" }) // "2024-07-01T00:00:00"
* @example roundDateTime("2024-06-15T12:34:56", { smallestUnit: "day" }) // "2024-06-16T00:00:00"
* @example roundDateTime("2024-06-15T12:34:56", { smallestUnit: "hour" }) // "2024-06-15T13:00:00"
* @example roundDateTime("invalid", { smallestUnit: "year" }) // ""
*/
export declare function roundDateTime(value: string, options: {
smallestUnit: DateTimeUnit;
roundingIncrement?: number;
roundingMode?: Temporal.RoundingMode;
}): string;