Working with Date Object0%

Working with Date Object

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Working with the Date Object in JavaScript

Handling timestamps, calculating elapsed time, and formatting dates are essential skills for building web applications (booking engines, analytics dashboards, expiration timers). JavaScript provides the built-in Date object to manage dates and times.


1. Creating Date Instances

A JavaScript Date object represents a single moment in time, measured in milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC).

JavaScript
// 1. Current date and time:
const now = new Date();
console.log(now); // e.g. Mon Sep 15 2026 10:45:00 GMT+0530
 
// 2. From an ISO 8601 date string:
const eventDate = new Date("2026-09-15T09:00:00");
 
// 3. From individual arguments: (Year, MonthIndex, Day, Hours, Minutes, Seconds)
// ⚠️ WARNING: Month is 0-indexed! 0 = January, 8 = September, 11 = December!
const customDate = new Date(2026, 8, 15, 10, 30);
 
// 4. From Epoch Milliseconds:
const fromEpoch = new Date(0); // Jan 01 1970

2. Getting Date & Time Components

JavaScript
const d = new Date();
 
console.log(d.getFullYear()); // 2026
console.log(d.getMonth()); // 8 (September, 0-indexed!)
console.log(d.getDate()); // 15 (Day of the month, 1-31)
console.log(d.getDay()); // 1 (Day of week: 0=Sunday, 1=Monday... 6=Saturday)
console.log(d.getHours()); // Current hour (0-23)
console.log(d.getMinutes()); // Current minute (0-59)
console.log(d.getSeconds()); // Current second (0-59)
console.log(d.getTime()); // Milliseconds since Jan 1, 1970

3. Calculating Time Differences (Elapsed Time)

Because subtracting dates yields the difference in milliseconds, time calculations are straightforward:

JavaScript
const startDate = new Date("2026-01-01");
const endDate = new Date("2026-01-11");
 
const diffInMs = endDate - startDate; // Milliseconds difference
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);
 
console.log(`Course duration: ${diffInDays} days`); // 10 days

4. Modern Formatting: Intl.DateTimeFormat

Instead of clumsy manual string assembly, modern browsers provide the powerful Internationalization API (Intl):

JavaScript
const today = new Date();
 
// Format for India (English):
const formatterIN = new Intl.DateTimeFormat("en-IN", {
dateStyle: "full",
timeStyle: "short"
});
console.log(formatterIN.format(today));
// "Tuesday, 15 September, 2026 at 10:45 AM"
 
// Format for United States:
const formatterUS = new Intl.DateTimeFormat("en-US", {
month: "short",
day: "numeric",
year: "numeric"
});
console.log(formatterUS.format(today));
// "Sep 15, 2026"

Interactive Quiz

Practice Quiz

Question 1
In the JavaScript Date object, what number represents the month of January?
Question 2
What does date.getDay() return?
Question 3
What is the "Unix Epoch"?
Question 4
How do you get the current timestamp in milliseconds without creating a full new Date() object?
Question 5
What modern built-in API is recommended for locale-sensitive, human-readable date and time formatting?
Next Lesson

Math Object Basics

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext