const specialInstant = new Temporal.Instant(1600184581000000000n);
console.log(
specialInstant.toLocaleString("en-US", { timeZone: "America/New_York" }),
);
// 9/15/2020, 11:43:01 AMRepresenting dates & times / notebook
Representing dates & times
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const specialInstant = new Temporal.Instant(-5694908400000000000n);
console.log(
specialInstant.toLocaleString("en-US", { timeZone: "Europe/Paris" }),
);
// 7/14/1789, 5:09:21 PMconst mdnCreationInst = new Temporal.Instant(1600184581000000000n);
const bastilleFallInst = new Temporal.Instant(-5694908400000000000n);
const mdnCreation = mdnCreationInst.toZonedDateTimeISO("America/New_York");
const bastilleFall = bastilleFallInst.toZonedDateTimeISO("Europe/Paris");
console.log(mdnCreation.toLocaleString("en-US")); // 9/15/2020, 11:43:01 AM EDT
console.log(bastilleFall.toLocaleString("en-US")); // 7/14/1789, 5:09:21 PM GMT+0:09:21console.log(mdnCreation.calendarId); // "iso8601"console.log(mdnCreation.year, mdnCreation.month, mdnCreation.day);
// 2020 9 15console.log(
mdnCreation.yearOfWeek,
mdnCreation.weekOfYear,
mdnCreation.dayOfWeek,
);
// 2020 38 2console.log(mdnCreation.year, mdnCreation.dayOfYear); // 2020 259const mdnCreationGreg = mdnCreation.withCalendar("gregory");
console.log(mdnCreationGreg.era, mdnCreationGreg.eraYear); // ce 2020const mdnCreationChi = mdnCreation.withCalendar("chinese");
console.log(mdnCreationChi.inLeapYear); // true
console.log(mdnCreationChi.month); // 8
console.log(mdnCreationChi.monthCode); // "M07"console.log(bastilleFall.timeZoneId); // "Europe/Paris"
console.log(bastilleFall.offsetNanoseconds); // 561000000000
console.log(bastilleFall.offset); // "+00:09:21" console.log(mdnCreation.offsetNanoseconds); // -14400000000000
console.log(mdnCreation.offset); // "-04:00"console.log(Temporal.Now.timeZoneId()); // e.g.: "Europe/Paris"console.log(mdnCreation.hour, mdnCreation.minute, mdnCreation.second);
// 11 43 1const mdnCreation = new Temporal.ZonedDateTime(
1600184581000000000n,
"America/New_York",
"gregory", // Defaults to "iso8601"
);const mdnCreation = Temporal.ZonedDateTime.from({
timeZone: "America/New_York", // Required
year: 2020, // Required
month: 9, // Required
day: 15, // Required
hour: 11, // Defaults to 0
minute: 43, // Defaults to 0
second: 1, // Defaults to 0
// The rest (millisecond, microsecond, nanosecond) default to 0
// calendar defaults to "iso8601"
});
console.log(mdnCreation.epochNanoseconds); // 1600184581000000000nconst mdnCreationDateTime = mdnCreation.toPlainDateTime();
const mdnCreationDate = mdnCreation.toPlainDate();
const mdnCreationTime = mdnCreation.toPlainTime();
console.log(mdnCreationDateTime.toLocaleString("en-US")); // "9/15/2020, 11:43:01 AM"
console.log(mdnCreationDate.toLocaleString("en-US")); // "9/15/2020"
console.log(mdnCreationTime.toLocaleString("en-US")); // "11:43:01 AM"const mdnCreationDate = Temporal.PlainDate.from({
year: 2020,
month: 9,
day: 15,
});const time1 = Temporal.ZonedDateTime.from({
timeZone: "America/New_York",
year: 2024,
month: 11,
day: 3,
});
const time2 = time1.add({ hours: 12 });
console.log(time2.toLocaleString("en-US")); // 11/3/2024, 11:00:00 AM ESTconst time1 = Temporal.ZonedDateTime.from({
timeZone: "America/New_York",
year: 2024,
month: 11,
day: 3,
});
const plainTime1 = time1.toPlainDateTime();
const plainTime2 = plainTime1.add({ hours: 12 });
const time2 = plainTime2.toZonedDateTime("America/New_York");
console.log(time2.toLocaleString("en-US")); // 11/3/2024, 12:00:00 PM ESTconst fakeDate = Temporal.ZonedDateTime.from({
timeZone: "America/New_York",
year: 2020,
month: 9,
day: 15,
// Time fields default to 0
});
console.log(fakeDate.subtract({ seconds: 1 }).day); // 14
console.log(fakeDate.add({ seconds: 1 }).day); // 15const realDate = Temporal.PlainDate.from({
year: 2020,
month: 9,
day: 15,
});
// Adding/subtracting durations less than a day is always ignored
console.log(realDate.subtract({ seconds: 1 }).day); // 15
console.log(realDate.add({ seconds: 1 }).day); // 15const newYear = Temporal.PlainDate.from({
year: 2026,
month: 12,
day: 31,
});
const reminder = newYear.toZonedDateTime({
plainTime: { hour: 23, minute: 59 },
timeZone: Temporal.Now.timeZoneId(),
});// A special whole month
const prideMonth2026 = Temporal.PlainYearMonth.from({ year: 2026, month: 6 });
// A special calendar date for every year
const remembranceDay = Temporal.PlainMonthDay.from({ month: 11, day: 11 });const juneteenth2026 = prideMonth2026.toPlainDate({ day: 19 }); // 2026-06-19
const remembranceDay2026 = remembranceDay.toPlainDate({ year: 2026 }); // 2026-11-11
const mdnCreationMonth = mdnCreationDate.toPlainYearMonth(); // 2020-09
const bastilleDay = bastilleFall.toPlainDate().toPlainMonthDay(); // 07-14const bastilleFallStr = bastilleFall.toString();
console.log(bastilleFallStr); // 1789-07-14T17:09:21+00:09[Europe/Paris]const mdnRepoHistory = JSON.stringify({ creation: mdnCreation });
console.log(mdnRepoHistory); // {"creation":"2020-09-15T11:43:01-04:00[America/New_York]"}const bastilleFall2 = Temporal.ZonedDateTime.from(bastilleFallStr); // Another ZDT objectconst mdnRepoHistoryParsed = JSON.parse(mdnRepoHistory, (key, value) => {
if (key === "creation") return Temporal.ZonedDateTime.from(value);
return value;
});
// { creation: ZonedDateTime {...} }const mdnCreation = Temporal.ZonedDateTime.from(
"2020-09-15T11:43:01-04:00[America/New_York]",
);
const bastilleFall = Temporal.ZonedDateTime.from(
"1789-07-14T17:00:00Z[Europe/Paris]",
);
const timeDiff = mdnCreation.since(bastilleFall);
console.log(
`mdn/content was created ${timeDiff.toLocaleString("en-US")} after the fall of the Bastille!`,
);
// mdn/content was created 2,026,414 hr, 43 min, 1 sec after the fall of the Bastille!
const timeDiff2 = bastilleFall.until(mdnCreation);
console.log(Temporal.Duration.compare(timeDiff, timeDiff2)); // 0 (equal)if (Temporal.ZonedDateTime.compare(mdnCreation, bastilleFall) > 0) {
console.log("mdn/content was created after the fall of the Bastille!");
} else {
console.log(
"What...? mdn/content is at least as old as the fall of the Bastille!",
);
}if (mdnCreation.equals(bastilleFall)) {
// This won't execute, because the ZDTs both represent different instants
// and have different time zone IDs
console.log(
"mdn/content was created at the exact same instant as the fall of the Bastille!",
);
}const mdnCreationHour = mdnCreation.round({ smallestUnit: "hours" });
console.log(
`mdn/content was created at approximately ${mdnCreationHour.toLocaleString("en-US")}`,
);
// mdn/content was created at approximately 9/15/2020, 12:00:00 PM EDTconst mdnCreationNoon = mdnCreation.with({ hour: 12, minute: 0, second: 0 });
const mdnCreationChi = mdnCreation.withCalendar("chinese");
const mdnCreationParis = mdnCreation.withTimeZone("Europe/Paris");const parisNow = Temporal.Now.zonedDateTimeISO("Europe/Paris");const dateObjectName = new Date();const xmas95 = new Date("1995-12-25");const today = new Date();
const endYear = new Date(today.getFullYear(), 11, 31, 23, 59, 59, 999);
const msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
let daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
daysLeft = Math.round(daysLeft); // Returns days left in the yearconst ipoDate = new Date();
ipoDate.setTime(Date.parse("Aug 9, 1995"));