Back to JavaScript Notes
Topic #317

JavaScript Dates

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.

  1. Create a Date with new Date() or new Date(string).
  2. Read parts with methods like getFullYear(), getHours(), or getTime().
  3. Convert to text with toISOString() or toLocaleString().
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 with Z.
  • getUTCFullYear() and getUTCHours() 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: use getUTCHours() when you need UTC.
  • Comparing Date objects with ==. Fix: compare getTime() 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.

NeedUseWhy
Read year, month, or dayDateProvides getters and setters.
Store in a database or sortTimestampA simple number is easy to compare.
Display localized textDate with Intl.DateTimeFormatHandles 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.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JavaScript Dates – FAQs

Quick answers about learning JavaScript Dates in JavaScript.

This free note from Coding Now Tech Institute explains JavaScript Dates in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including JavaScript Dates, is 100% free with no signup required.
With focused practice, most students grasp JavaScript Dates in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now