JavaScript represents dates and times using the built-in Date object. Under the hood, a Date instance stores a single integer representing milliseconds elapsed since the Unix Epoch (January 1, 1970, 00:00:00 UTC).
flowchart TD
Epoch["Unix Epoch: Jan 1, 1970 00:00:00 UTC (0 ms)"] --> Inst1["new Date() -> Current Client Timestamp"]
Epoch --> Inst2["new Date(timestamp) -> Exact Epoch Milliseconds"]
Epoch --> Inst3["new Date('YYYY-MM-DD') -> Parsed ISO Date"]
| Constructor Call | Input Format | Result |
|---|---|---|
new Date() |
None | Current date and time according to client environment. |
new Date(ms) |
Number | Date calculated by adding ms to Unix Epoch. |
new Date(dateString) |
ISO 8601 String | Date parsed from formatted string ("2026-08-13T12:00:00Z"). |
new Date(y, m, d, h, m, s) |
Numeric Components | Date created from components (Note: Month is 0-indexed: 0 = Jan). |
// Demonstrating Date creation, UTC vs Local time, and Timestamps
// 1. Current Date & Unix Timestamp
const now = new Date();
console.log(`Current Date String: ${now.toString()}`);
console.log(`Unix Epoch Timestamp (ms): ${now.getTime()}`); // Or Date.now()
// 2. Creating Specific Dates (Watch 0-indexed Months!)
// August 13, 2026 -> Month index 7 (0=Jan, 1=Feb ... 7=Aug)
const specificDate = new Date(2026, 7, 13, 15, 30, 0);
console.log(`Local Time Output: ${specificDate.toLocaleString()}`);
console.log(`UTC Time Output: ${specificDate.toUTCString()}`);
// 3. ISO 8601 UTC String Parsing
const isoDate = new Date("2026-08-13T12:00:00Z");
console.log(`Parsed ISO Year: ${isoDate.getUTCFullYear()}`);
// 4. Calculating Time Elapsed Between Timestamps
const startTimestamp = Date.now();
// Simulating workload delay
const endTimestamp = Date.now();
console.log(`Elapsed Execution Time: ${endTimestamp - startTimestamp} ms`);
0 is January and 11 is December. Passing new Date(2026, 8, 1) creates a date in September, not August.Date.now() for Fast Timestamps: Calling Date.now() is faster than creating an unnecessary instance with new Date().getTime().Write a function getDaysDifference(date1, date2) that returns the whole number of days elapsed between two Date objects.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.