By the end of this lesson, you will be able to create, read, compare, and format JavaScript Date objects for common time-based tasks.
What it is
A JavaScript Date object represents a single point in time as milliseconds since the Unix epoch: 1970-01-01T00:00:00Z. Internally, the value is UTC-based, but many methods display or interpret it in the runtime's local time zone. Related terms include timestamp, ISO 8601 string, UTC, and local time.
Why it matters
- It lets you work with real-world calendar values such as years, months, days, hours, and minutes.
- It supports sorting and filtering records by time, such as posts, events, or logs.
- It helps convert between machine-friendly timestamps and human-readable strings.
- It provides built-in methods for timezone-aware calculations when used carefully.
Syntax or steps
The smallest useful pattern is to create a Date, read it, and convert it to a standard string.
- Create a Date with
new Date()ornew Date(string). - Read parts with methods like
getFullYear(),getHours(), orgetTime(). - Convert to text with
toISOString()ortoLocaleString().
const now = new Date();
const timestamp = now.getTime();
const isoString = now.toISOString();
const parsed = new Date(isoString);
Example
const now = new Date();
const event = new Date('2025-06-01T09:30:00Z');
console.log(now.toISOString());
console.log(event.getUTCFullYear());
console.log(event.getUTCHours());
console.log(event.getTime() > now.getTime());
new Date()creates a Date for the current moment.new Date('2025-06-01T09:30:00Z')parses an ISO string as UTC time.toISOString()returns a UTC string ending withZ.getUTCFullYear()andgetUTCHours()read UTC-based fields.getTime()returns milliseconds, which makes comparison reliable.
Common mistakes
- Parsing ambiguous strings like
new Date('06/01/2025'). Fix: use ISO 8601 or explicit components. - Assuming
getHours()is UTC. Fix: usegetUTCHours()when you need UTC. - Comparing Date objects with
==. Fix: comparegetTime()values or use<and>carefully. - Forgetting that setters mutate the original Date. Fix: copy it first with
new Date(original).
When to use it
Use Date when you need calendar fields, formatting, or timezone-aware methods. Use a numeric timestamp when you only need storage, sorting, or arithmetic.
| Need | Use | Why |
|---|---|---|
| Read year, month, or day | Date | Provides getters and setters. |
| Store in a database or sort | Timestamp | A simple number is easy to compare. |
| Display localized text | Date with Intl.DateTimeFormat | Handles locale and timezone formatting. |
Practice
Guided exercise: create a Date for July 4, 2025 at noon UTC, then print its ISO string.
const holiday = new Date('2025-07-04T12:00:00Z');
console.log(holiday.toISOString());
Expected output: 2025-07-04T12:00:00.000Z.
Challenge: write a function that returns true if a date is in the future. Hint: compare date.getTime() with Date.now().
Quick check
Question: Why can new Date('2025-01-01') and new Date('2025-01-01T00:00:00') behave differently?
Answer: The first is treated as UTC midnight by modern JavaScript, while the second without Z is treated as local midnight, so the resulting timestamp can differ by the local offset.
Summary
JavaScript Dates are millisecond-based objects that represent points in time. They are most reliable when you use ISO strings, UTC methods, and getTime() for comparisons. Understanding the difference between UTC and local time prevents many common bugs.