Below, we have two wall-clock times that we want to interpret in the America/New_York
time zone. The first one, dtNotExist
, never existed because of a forward daylight saving time shift, so we need to choose from the times 01:05:00-05:00
or 03:05:00-04:00
. The second one, dtAmbiguous
, appeared twice because of a backward daylight saving time shift, so we need to choose from the times 01:05:00-04:00
or 01:05:00-05:00
. For a more detailed explanation of this situation, see ambiguity and gaps from local time to UTC time.
const dtNotExist = Temporal.PlainDateTime.from("2024-03-10T02:05:00");
const dtAmbiguous = Temporal.PlainDateTime.from("2024-11-03T01:05:00");
// Default: compatible
console.log(dtNotExist.toZonedDateTime("America/New_York").toString());
// '2024-03-10T03:05:00-04:00[America/New_York]'
console.log(dtAmbiguous.toZonedDateTime("America/New_York").toString());
// '2024-11-03T01:05:00-04:00[America/New_York]'
// Use the earlier time for ambiguous times
console.log(
dtNotExist
.toZonedDateTime("America/New_York", { disambiguation: "earlier" })
.toString(),
);
// '2024-03-10T01:05:00-05:00[America/New_York]'
console.log(
dtAmbiguous
.toZonedDateTime("America/New_York", { disambiguation: "earlier" })
.toString(),
);
// '2024-11-03T01:05:00-04:00[America/New_York]'
// Use the later time for ambiguous times
console.log(
dtNotExist
.toZonedDateTime("America/New_York", { disambiguation: "later" })
.toString(),
);
// '2024-03-10T03:05:00-04:00[America/New_York]'
console.log(
dtAmbiguous
.toZonedDateTime("America/New_York", { disambiguation: "later" })
.toString(),
);
// '2024-11-03T01:05:00-05:00[America/New_York]'
// Throw an error for ambiguous times
dtNotExist.toZonedDateTime("America/New_York", { disambiguation: "reject" });
// RangeError: instant is ambiguous