The TZDate class (and TZDateMini) allows creating date instances tied to a specific time zone.
Constructor
Mirrors the standard Date constructor but accepts an optional time zone as the last argument:
new TZDate(year, month, day, timeZone)new TZDate(timestamp, timeZone)new TZDate(dateString, timeZone)
TZDate.tz()
Static method to construct a TZDate instance with a specific time zone. Unlike the constructor, this method expects the time zone as the first argument:
TZDate.tz(timeZone)TZDate.tz(timeZone, year, month)TZDate.tz(timeZone, timestamp)TZDate.tz(timeZone, dateString)
timeZone
Readonly property returning the assigned time zone name (e.g., "Asia/Singapore"). Returns undefined if no time zone was provided.
withTimeZone(timeZone)
Returns a new TZDate instance representing the same moment in time but in the specified timeZone.
[Symbol.for("constructDateFrom")]
Exposes a method to construct a standard Date instance in the same time zone. This is used by date-fns internally but can be accessed via import { constructFromSymbol } from "@date-fns/tz".
import { TZDate, constructFromSymbol } from "@date-fns/tz";
// Constructor
const sg = new TZDate(2022, 2, 13, "Asia/Singapore");
// TZDate.tz
const nowInSg = TZDate.tz("Asia/Singapore");
// timeZone property
console.log(sg.timeZone); // "Asia/Singapore"
// withTimeZone
const ny = sg.withTimeZone("America/New_York");
// constructDateFrom
const symbol = Symbol.for("constructDateFrom");
const date = sg[symbol](new Date(2024, 0, 1));