JavaScript Tutorial

JavaScript Date Object: Create, Read, Change and Format Dates

TGTeam GyaniPanditNovember 13, 20265 min read

Hello friends! Almost every app needs dates somewhere, whether it is showing today’s date, counting days to an exam or printing the time on a message. JavaScript’s built-in Date object lets you work with dates and times: today’s date, a countdown to an exam, a person’s age, or a timestamp on a message. Inside, a Date is just one number, the milliseconds since 1 January 1970 (UTC). The methods let you read and change that moment in human terms.

In the examples below, outputs that depend on the time zone are shown for Indian Standard Time (IST, UTC+5:30). So let’s begin with the very first step: creating a date.

Creating a Date

const d1 = new Date(2026, 0, 26);            // year, month (0-based), day
console.log(d1.toDateString());

const d2 = new Date(2026, 0, 26, 9, 30);     // with hours and minutes
console.log(d2.toString());

const d3 = new Date("2026-08-15");           // ISO date string (treated as UTC)
console.log(d3.toISOString());

const d4 = new Date("2026-08-15T10:00:00+05:30");
console.log(d4.toISOString());

const d5 = new Date(0);                       // milliseconds since 1 Jan 1970 UTC
console.log(d5.toISOString());

Output:

Mon Jan 26 2026
Mon Jan 26 2026 09:30:00 GMT+0530 (India Standard Time)
2026-08-15T00:00:00.000Z
2026-08-15T04:30:00.000Z
1970-01-01T00:00:00.000Z

new Date() with no arguments gives the current date and time. Months start from 0: January is 0 and December is 11. This catches almost every beginner at least once.

Once you have a Date, the next thing you will usually want is to pull out a part of it, like just the year or just the hour.

Reading Parts of a Date (Getters)

const d = new Date(2026, 10, 14, 18, 45, 30);   // 14 Nov 2026, 6:45:30 PM
console.log(d.getFullYear(), d.getMonth(), d.getDate(), d.getDay());
console.log(d.getHours(), d.getMinutes(), d.getSeconds());
console.log(d.getTime());

const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
console.log(days[d.getDay()]);

Output:

2026 10 14 6
18 45 30
1794662130000
Sat
Method Returns
getFullYear() 4-digit year
getMonth() 0 to 11
getDate() day of the month, 1 to 31
getDay() day of the week, 0 (Sunday) to 6 (Saturday)
getHours(), getMinutes(), getSeconds() time parts
getTime() milliseconds since 1 Jan 1970 UTC

Each getter also has a UTC version, such as getUTCHours(), which ignores your local time zone.

Reading is one half of the story. The other half is changing a date, for example moving it 5 days ahead.

Changing a Date (Setters)

const f = new Date(2026, 11, 30);
f.setDate(f.getDate() + 5);         // add 5 days
console.log(f.toDateString());

const e = new Date(2026, 0, 31);
e.setMonth(1);                      // try to move 31 Jan to February
console.log(e.toDateString());

Output:

Mon Jan 04 2027
Tue Mar 03 2026

Dates roll over automatically. Adding 5 days to 30 December correctly moves into January of the next year. But there is no 31 February, so in the second example JavaScript rolls forward to 3 March. Keep this in mind when you add months.

Now comes a part that matters a lot for us in India: showing the date in the format people here are used to.

Formatting Dates for Indian Users

const g = new Date(2026, 7, 15, 9, 5);
console.log(g.toLocaleDateString("en-IN"));
console.log(g.toLocaleDateString("en-IN", { day: "numeric", month: "long", year: "numeric" }));
console.log(g.toLocaleTimeString("en-IN", { hour: "2-digit", minute: "2-digit" }));
console.log(g.toLocaleDateString("hi-IN", { weekday: "long", day: "numeric", month: "long" }));

Output:

15/8/2026
15 August 2026
09:05 am
शनिवार, 15 अगस्त

toLocaleDateString() with the "en-IN" locale gives the day-month-year order used in India, and "hi-IN" even gives Hindi day and month names.

Friends, you now know enough to build something useful. Let’s try two small programs.

Practical Examples

1. Days Left Until an Exam

const start = new Date(2026, 10, 9);   // 9 Nov 2026
const exam = new Date(2027, 1, 15);    // 15 Feb 2027
const ms = exam - start;
console.log(Math.round(ms / (1000 * 60 * 60 * 24)));

Output:

98

Subtracting two dates gives the difference in milliseconds. Divide by the milliseconds in a day to get days.

2. Calculate Age

function age(dob, today) {
  let a = today.getFullYear() - dob.getFullYear();
  const m = today.getMonth() - dob.getMonth();
  if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) a--;
  return a;
}
console.log(age(new Date(2008, 11, 20), new Date(2026, 10, 9)));

Output:

17

The birthday (20 December) has not come yet in 2026, so one year is subtracted.

Common Mistakes

Dates are one area where even experienced programmers slip up. Here are the mistakes to watch out for:

const bad = new Date("31/12/2026");      // DD/MM/YYYY is not understood
console.log(bad.toString(), isNaN(bad));

console.log(new Date(2026, 1, 30).toDateString());   // 30 February?

const a1 = new Date(2026, 0, 1);
const a2 = new Date(2026, 0, 1);
console.log(a1 == a2, a1.getTime() === a2.getTime());

Output:

Invalid Date true
Mon Mar 02 2026
false true
  • Indian date format strings are not parsed. Use new Date(2026, 11, 31) or the ISO format "2026-12-31". Check for isNaN(date) when reading user input.
  • Invalid days do not throw errors; they roll over silently, as 30 February became 2 March here.
  • Comparing dates with == compares objects, not moments. Use getTime(), or < and >, which work directly on dates.
  • “2026-08-15” (date only) is read as UTC midnight, while "2026-08-15T00:00" (with a time) is read as local time. Be consistent.

So friends, the three things to remember are simple. Months start from 0, so November is 10 and you add 1 when you display it. new Date() gives you today’s date, and toLocaleDateString("en-IN") shows it the Indian way. And to find the days between two dates, subtract them and divide by 1000 * 60 * 60 * 24, then round the result. Practise with your own birthday or your next exam date, and the Date object will soon feel easy.

Related: JavaScript Map keys() · JavaScript Set has() · Arrow functions in JavaScript

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses