Skip to content

convertPlainDateTimeToZoned

convertPlainDateTimeToZoned(value: string, timeZone: string, optionsArg?: { smallestUnit?: SmallestUnit<"minute" | "second" | "millisecond" | "microsecond" | "nanosecond"> | undefined; disambiguation?: "compatible" | "earlier" | "later" | "reject" | undefined; }): string

Attach the specified timeZone to a plain datetime string and return a zoned ISO 8601 datetime string.

  • Combines plain datetime with timezone to create ZonedDateTime.
  • There is no offset option (removed in 1.16.0): Temporal PlainDateTime#toZonedDateTime reads only disambiguation, and a plain datetime has no UTC offset for offset to act on.
  • Output is cut to milliseconds by default, so microseconds and nanoseconds in value are dropped. Pass { smallestUnit: "nanoseconds" } to keep every digit.
  • Returns "" for invalid input.
  • An offset with seconds (-00:44:30) returns "". A written zone cannot carry seconds (RFC 9557 §4.1), so the result could not name it. Pass the IANA name, or call resolveLocal for the instant the wall time names at that offset.
ParameterTypeDescription
valuestringplain datetime string (e.g. “2024-02-29T14:30:45”)
timeZonestringIANA name or a UTC offset to the minute (what isValidTimeZone accepts)

optionsArg

optional settings for the precision of the output string and for resolving a DST gap or overlap

OptionTypeDefaultDescription
smallestUnit?SmallestUnit<"minute" | "second" | "millisecond" | "microsecond" | "nanosecond">"milliseconds"The smallest unit written in the output string, from "minute" to "nanosecond", singular or plural. Anything smaller is truncated, as Temporal’s toString does with its "trunc" rounding mode.
disambiguation?"compatible" | "earlier" | "later" | "reject" (Disambiguation)"compatible", Temporal’s default.How value resolves when its wall-clock time falls in a DST gap or overlap in timeZone. In a fall-back overlap "compatible" and "earlier" take the earlier instant and "later" the later one; in a spring-forward gap "compatible" and "later" move the wall clock forward by the gap length and "earlier" back by it. "reject" returns "" for both.

zoned ISO 8601 datetime string or "" when invalid

import { convertPlainDateTimeToZoned } from "@northguild/gmt/zoned/convert";
convertPlainDateTimeToZoned("2024-02-29T14:30:45", "America/New_York") // "2024-02-29T14:30:45.000-05:00[America/New_York]"
output 

zoned/convert/convertPlainDateTimeToZoned.ts