Skip to content

roundDate

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

Round an ISO 8601 date string to the specified date unit.

  • Returns “” for invalid inputs.
  • Accepts date units: “year”, “month”, “week”, “day”.
  • Time units (“hour”, “minute”, etc.) are rejected and return “”.
  • All date units use manual start-of-unit rounding.
  • Wraps all Temporal calls in try-catch; returns “” on any error.
Parameter Type Description
value string ISO 8601 date string

options

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

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

roundDate("2024-06-15", { smallestUnit: "year" }) // "2024-01-01"
roundDate("2024-06-15", { smallestUnit: "month" }) // "2024-07-01"
roundDate("2024-06-15", { smallestUnit: "week" }) // "2024-06-16"
roundDate("2024-06-15", { smallestUnit: "day" }) // "2024-06-15"
roundDate("invalid", { smallestUnit: "year" }) // ""

plain/calculate/roundDate.ts