Skip to content

setDateTime

setDateTime(value: string, fields: Omit<PlainDateTimeLike, "calendar" | "era" | "eraYear">, options?: { overflow?: "reject" | "constrain" | undefined; }): string

Return a PlainDateTime ISO string with the given fields set on value.

  • Wraps Temporal.PlainDateTime.prototype.with(), which resolves every supplied field in a single atomic overflow pass — see setDate’s doc for why that matters over composing addDateTime() calls field-by-field.
  • fields may set any of year, month, monthCode, day, hour, minute, second, millisecond, microsecond and/or nanosecond; omitted fields keep their current value. An object with no field set, such as {} or { year: undefined }, is a no-op.
  • Values are not converted: monthCode must be a string and every other field an integer number. null, a numeric string, a boolean or a fraction returns "", where Temporal would convert or truncate it. A bigint, NaN or an infinity returns "" too. A number too large for its field follows overflow. A month or day below 1, or a date outside Temporal’s range, returns "".
  • An own key that is not one of these fields, such as a misspelt yaer, returns "". So does an era, or a calendar or timeZone property, whether own, inherited or a getter. A field is read with a property get, as Temporal reads it, so an inherited or getter field is set.
  • A Temporal date or time object returns "", as Temporal’s with refuses one. One whose prototype has been replaced with Object.prototype, null or Function.prototype sets nothing instead.
  • Returns "" for invalid input.
ParameterTypeDescription
valuestringISO PlainDateTime string
fieldsOmit<PlainDateTimeLike, "calendar" | "era" | "eraYear">Partial<Temporal.PlainDateTimeLike> object specifying fields to set

options

How an out-of-range field value is handled

OptionTypeDefaultDescription
overflow?"reject" | "constrain" (Overflow)"constrain", Temporal’s default.What to do when the fields do not form a real date-time. "constrain" clamps each field to its nearest valid value, so month: 2 on the 31st gives Feb 29 or 28; "reject" returns "".

ISO PlainDateTime string with fields set, or "" on invalid input

import { setDateTime } from "@northguild/gmt/plain/calculate";
setDateTime("2024-03-10T12:00:00", { hour: 9 }) // "2024-03-10T09:00:00"
output 

plain/calculate/setDateTime.ts