Summary - Date and time
- Node.js course
- Intermediate
- Lesson 99/116
- 5 min
Combine Date, Intl and setTimeout to write a timer.
Summary - Date and time
In this group you learned Node.js's date and time tools: the Date object, getUTCDate/getUTCMonth methods, localization with Intl.DateTimeFormat, high-resolution measurement with performance.now, and setTimeout/setInterval timers. Now we will combine them in a timer.
Structure: show the UTC date localized with Intl.DateTimeFormat, and use setTimeout to print a message after a delay. This is a small example of combining date display and a scheduled task in a real app. Even with a 0 ms delay the timer runs after sync code, so the output order is deterministic.
Practice Write a localized date and timer
Define const date = new Date("2024-03-15T00:00:00Z");. Create const fmt = new Intl.DateTimeFormat("tr-TR", { day: "numeric", month: "long", year: "numeric", timeZone: "UTC" });. Print the date with console.log(fmt.format(date)). Set a timer with setTimeout(() => console.log("Timer done"), 0);.
Starter code
// Define a UTC date
// write here
// Create a tr-TR formatter
// write here
// Print the date
// write here
// Print "Timer done"
// write hereSample solution
const date = new Date("2024-03-15T00:00:00Z");
const fmt = new Intl.DateTimeFormat("tr-TR", { day: "numeric", month: "long", year: "numeric", timeZone: "UTC" });
console.log(fmt.format(date));
setTimeout(() => console.log("Timer done"), 0);Expected output
15 Mart 2024 Zamanlayıcı tamam
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the best way to show a date in Turkish format?
new Intl.DateTimeFormat("tr-TR", ...)- Intl.DateTimeFormat("tr-TR", ...) kullanmak Correct answer
- Building an array of month names by hand
- Date.toString() kullanmak
- performance.now() kullanmak
Explanation: Intl.DateTimeFormat does localized formatting.
2 Match each tool to its purpose
| Term | Meaning |
|---|---|
Date |
Date object |
getUTCMonth |
Month component |
Intl.DateTimeFormat |
Localized formatting |
setTimeout |
Delayed call |
3 Complete the DateTimeFormat call
const fmt = new Intl.____("tr-TR", { day: "numeric", month: "long", year: "numeric", timeZone: "UTC" });
const fmt = new Intl.DateTimeFormat("tr-TR", { day: "numeric", month: "long", year: "numeric", timeZone: "UTC" });
Explanation: Intl.DateTimeFormat creates a local formatter.