JavaScript's classic Date object has confused students for years: months start at 0 instead of 1, methods mutate dates in place, and converting across time zones often requires extra libraries. Starting in Node.js 26 LTS, the modern Temporal API is enabled by default with no flags or npm packages needed. In this lesson, you will build a clean assignment deadline helper using Temporal.
Temporal.PlainDate (Calendar date only) • Temporal.ZonedDateTime (Exact date, time & time zone) • Temporal.Now (Current clock)
Why Temporal Replaces Legacy Date
- Immutable by default: Calling
.add({ days: 7 })returns a new object and never silently alters the original date. - 1-based months: January is
1and December is12, just like ISO strings and calendars. - Built-in IANA time zones: Convert class schedules cleanly between
Asia/Kolkata,America/New_York, orEurope/Londonincluding Daylight Saving rules.
Try a Student Deadline Script in Node.js 26
Check your Node version (node -v should be v26+), save this code as deadlines.mjs, and run node deadlines.mjs:
// 1. Work with calendar dates (no unexpected time-of-day shifts)
const assignedDate = Temporal.Now.plainDateISO();
const dueDate = assignedDate.add({ days: 14 });
console.log("Assigned:", assignedDate.toString());
console.log("Due Date:", dueDate.toString());
// 2. Convert a live study session across time zones
const liveClassIST = Temporal.ZonedDateTime.from(
"2026-12-01T18:30:00[Asia/Kolkata]"
);
const liveClassEST = liveClassIST.withTimeZone("America/New_York");
console.log("India Time:", liveClassIST.toString());
console.log("New York Time:", liveClassEST.toString());
// 3. Safe month math (clamps Jan 31 + 1 month to Feb 28)
const janEnd = Temporal.PlainDate.from("2026-01-31");
console.log("Next Month:", janEnd.add({ months: 1 }).toString());
Understand the Three Useful Patterns
Temporal.Now.plainDateISO()gives today's date without hours or time zones, which is ideal for due dates and birthdays.Temporal.ZonedDateTime.from(...)attaches an explicit IANA zone name so.withTimeZone(...)computes the exact local time for remote students.janEnd.add({ months: 1 })safely clamps to2026-02-28instead of rolling over into March like legacyDatedoes.
Practice
- Use
assignedDate.until(dueDate)to print how many days remain until the assignment deadline. - Create a
Temporal.PlainDatefor your next exam and add{ weeks: 2, days: 3 }without mutating the original variable. - Convert
liveClassISTintoAsia/SingaporeandEurope/Londonusing.withTimeZone(...).
Continue learning: Node.js Tutorials and Examples.
References: Node.js 26.0.0 Release Notes; MDN Temporal Reference.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.