Dates
new Date
The Date constructor captures the current moment down to milliseconds. Store the object when calendar fields are needed later.
const now: Date = new Date(); console.log(now.getFullYear());
Methods read year, month, and day from the stored moment. For raw speed, the numeric timestamp from the next sections is simpler.
toISOString
Logs need one stable order that sorts well and reads the same everywhere. The ISO method prints year first in UTC with millisecond precision.
const now: Date = new Date("2026-03-14T09:30:00.000Z"); console.log(now.toISOString());
2026-03-14T09:30:00.000Z
Timestamps math
The now function returns milliseconds since the epoch as a plain number. Subtract two stamps to measure elapsed time without date objects.
const start: number = new Date("2026-03-14T09:30:00.000Z").getTime(); const stop: number = new Date("2026-03-14T09:30:02.500Z").getTime(); const elapsed: number = stop - start; console.log(elapsed);
2500
Addition and subtraction are exact on these counts. Save stamps around slow work to find real costs instead of guessing.
Weekday names
The day method returns zero for Sunday through six for Saturday. Index a name table with that number to show friendly labels.
const names: string[] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; const today: Date = new Date(2026, 2, 14); console.log(names[today.getDay()] ?? "?");
Sat
The table order must start with Sunday to match the numeric contract. A fallback keeps the print safe even if the index ever surprises.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
Date.now()return?Answer
Milliseconds since the <a href="/unix/">Unix</a> epoch
The call gives a plain number, which is why two stamps subtract cleanly.
-
What order does
toISOString()print?Answer
Year, month, and day in UTC with milliseconds
The ISO text starts with the year and ends with the Z that marks UTC.
-
What range does
getDay()return?Answer
0 for Sunday through 6 for Saturday
The week starts at Sunday zero, which is why the name table is ordered that way.
-
Why measure elapsed time with two timestamps?
Answer
Subtracting the numbers gives the duration exactly
The millisecond counts are plain numbers, so the difference is the elapsed time.
-
What does this program print?
const now: Date = new Date("2026-03-14T09:30:00.000Z"); console.log(now.toISOString());Answer
2026-03-14T09:30:00.000Z
The date is built from that exact ISO text, and the method prints it back.
-
What does this program print?
const start: number = new Date(0).getTime(); const stop: number = new Date(1500).getTime(); console.log(stop - start);Answer
1500
The two millisecond stamps differ by 1500.
-
What does this program print?
console.log(new Date(2026, 2, 14).getDay());Answer
6
March 14, 2026 is a Saturday, and Saturday is index 6.
-
What does this program print?
const names = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; console.log(names[6] ?? "?");Answer
Sat
Index 6 is the last name in the Sunday-first table.
-
What does the compiler say about this program?
const d: Date = Date.now(); console.log(d);Answer
error TS2322: Type 'number' is not assignable to type 'Date'.
Date.now gives a number, while the variable is declared as a Date.
-
What does the compiler say about this program?
const n: number = new Date(); console.log(n);Answer
error TS2322: Type 'Date' is not assignable to type 'number'.
The constructor gives a Date object, and a number variable cannot hold it.
Article author: Arthur Isaev