Skip to content

Time zones and UTC

Every instance is in a time zone: the system’s by default, UTC, or any IANA time zone. In Day.js these are the utc and timezone plugins; in yuga they’re part of core.

yuga.tz("2024-05-15 10:20", "Asia/Tokyo"); // 10:20 in Tokyo.
yuga().tz("America/New_York"); // Now, in New York.
yuga().tz("America/New_York").hour(); // The hour in New York.

yuga.tz(value, zone) reads the value as the wall clock in that time zone. instance.tz(zone) is the same instant, seen in another time zone. Without an argument, tz() is the instance’s time zone.

yuga.utc("2024-05-15 10:20").format(); // "2024-05-15T10:20:00Z"
yuga().utc(); // Now, in UTC.
yuga.utc("2024-05-15T10:20:00Z").local(); // The same instant, in the system's time zone.
const paris = yuga.tz("2024-05-15 10:20", "Europe/Paris");
paris.utcOffset(); // 120, minutes ahead of UTC
paris.offsetName(); // "GMT+2"
paris.utcOffset(-300); // The same instant at -05:00.

Units of a day or longer move the wall clock, so they keep the time of day across a change of clocks. Shorter units move the instant:

const night = yuga.tz("2024-03-31 01:30", "Europe/Paris"); // The night clocks go forward.
night.add(1, "hour").format(); // "2024-03-31T03:30:00+02:00"
night.add(1, "day").format(); // "2024-04-01T01:30:00+02:00"

yuga.tz.setDefault(zone) is the time zone of every new instance that doesn’t name one. yuga.tz.guess() is the system’s time zone.

yuga reads the system’s time zone and UTC off Date, and caches the offsets of other time zones between their transitions, so it doesn’t build an Intl formatter or a Temporal value on each call. See Performance and size.