Skip to content

roundDateTime

roundDateTime(value: string, options: { smallestUnit: DateTimeUnit; roundingIncrement?: number; roundingMode?: Temporal.RoundingMode; }): string
import { roundDateTime } from "@northguild/gmt/plain/calculate";

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.
Parameter Type Description
value string ISO 8601 datetime string

options

Option Type Default
smallestUnit DateTimeUnit
roundingIncrement? number
roundingMode? Temporal.RoundingMode

Rounded ISO 8601 datetime string, or “” on invalid input

roundDateTime("2024-06-15T12:34:56", { smallestUnit: "year" }) // "2024-01-01T00:00:00"
roundDateTime("2024-06-15T12:34:56", { smallestUnit: "month" }) // "2024-07-01T00:00:00"
roundDateTime("2024-06-15T12:34:56", { smallestUnit: "day" }) // "2024-06-16T00:00:00"
roundDateTime("2024-06-15T12:34:56", { smallestUnit: "hour" }) // "2024-06-15T13:00:00"
roundDateTime("invalid", { smallestUnit: "year" }) // ""

plain/calculate/roundDateTime.ts