Skip to content
Laviyx Learn

Try

JavaScript course menu

JavaScript Dates and Times: The Date Object, Formatting and Time Zones

Work with dates in JavaScript: creating Date objects, timestamps, getting and setting parts of a date (and the zero-based month), date arithmetic, comparing dates, formatting with toLocaleDateString and Intl.DateTimeFormat, ISO strings, time zones, and the upcoming Temporal API.

Beginner8 min readUpdated
On this page

Dates appear in almost every application: order times, due dates, birthdays, "posted 3 days ago". JavaScript's built-in Date object handles them, with a few quirks worth knowing.

Creating dates#

JavaScript
const fromIso = new Date("2026-11-14T09:30:00Z");
const fromParts = new Date(Date.UTC(2026, 10, 14, 9, 30));   // month 10 = November!
const fromTimestamp = new Date(0);

console.log(fromIso.toISOString());
console.log(fromParts.toISOString());
console.log(fromTimestamp.toISOString());
Output
2026-11-14T09:30:00.000Z
2026-11-14T09:30:00.000Z
1970-01-01T00:00:00.000Z
  • An ISO 8601 string (YYYY-MM-DDTHH:mm:ssZ) is the safest way to create a date from text.
  • Months are numbered from 0: January is 0 and December is 11. Days of the month start at 1. This trips up everyone.
  • A timestamp is the number of milliseconds since 1 January 1970, 00:00 UTC.

new Date() with no arguments gives the current date and time, and Date.now() gives the current timestamp:

JavaScript
console.log(new Date());     // the current date and time
console.log(Date.now());     // milliseconds since 1970, e.g. for timing code

Reading parts of a date#

Each part has a local-time getter and a UTC getter:

JavaScript
const d = new Date("2026-11-14T09:30:00Z");

console.log(d.getUTCFullYear());
console.log(d.getUTCMonth());       // 0-based
console.log(d.getUTCDate());        // day of the month
console.log(d.getUTCDay());         // day of the week: 0 = Sunday
console.log(d.getUTCHours(), d.getUTCMinutes());
console.log(d.getTime());
Output
2026
10
14
6
9 30
1794648600000

The local versions (getFullYear, getMonth, getHours and so on) use the time zone of the computer running the code.

Date arithmetic#

Dates are stored as timestamps, so subtracting two dates gives milliseconds:

JavaScript
const start = new Date("2026-10-24T00:00:00Z");
const exam = new Date("2026-12-01T00:00:00Z");

const msPerDay = 24 * 60 * 60 * 1000;
const days = Math.round((exam - start) / msPerDay);
console.log(`${days} days until the exam`);

const dueDate = new Date(start);
dueDate.setUTCDate(dueDate.getUTCDate() + 30);   // add 30 days
console.log(dueDate.toISOString().slice(0, 10));
Output
38 days until the exam
2026-11-23

Setters such as setUTCDate roll over correctly: adding 30 days to 24 October lands in November. Note that setters change the Date object, which is why the example copies start first with new Date(start).

Comparing dates#

JavaScript
const a = new Date("2026-01-01T00:00:00Z");
const b = new Date("2026-01-01T00:00:00Z");

console.log(a < new Date("2026-06-01T00:00:00Z"));
console.log(a === b);
console.log(a.getTime() === b.getTime());
Output
true
false
true

< and > work, but === compares object references, so compare getTime() values for equality.

Formatting dates for people#

toLocaleDateString and Intl.DateTimeFormat format dates for any language and region:

JavaScript
const d = new Date("2026-11-14T09:30:00Z");

console.log(d.toLocaleDateString("en-IN", { timeZone: "UTC" }));
console.log(d.toLocaleDateString("en-US", { timeZone: "UTC" }));
console.log(d.toLocaleDateString("en-GB", { dateStyle: "long", timeZone: "UTC" }));
console.log(
  new Intl.DateTimeFormat("en-IN", {
    weekday: "long",
    day: "numeric",
    month: "long",
    hour: "numeric",
    minute: "2-digit",
    timeZone: "Asia/Kolkata",
  }).format(d)
);
Output
14/11/2026
11/14/2026
14 November 2026
Saturday, 14 November at 3:00 pm

The same moment is 09:30 in UTC and 15:00 in India (UTC+5:30), which is why the last line shows 3:00 pm. Always format dates with these APIs rather than building strings by hand. (The output above is from Node.js; small details such as commas and the word "at" come from the locale data built into each browser and can differ slightly between versions.)

Time zones#

A Date stores one moment in time. What clock time that moment shows depends on the time zone. A few rules avoid most bugs:

  • Store and send dates in UTC, as ISO strings (toISOString()), especially in databases and APIs.
  • Convert to local time only for display, with toLocaleString and a timeZone option when needed.
  • Be careful with date-only strings: new Date("2026-11-14") is parsed as midnight UTC, which can show as 13 November in time zones behind UTC.

The Temporal API#

Because Date has so many quirks, JavaScript is gaining a new date and time API called Temporal, with separate types for plain dates, times, time zones and durations. It has started shipping in browsers but is not yet available everywhere. Until it is, Date with Intl (or a small library such as date-fns or Day.js) is the standard choice.

Practice#

Practice exercise

A course started on 2026-09-01T00:00:00Z and lasts 75 days. Print the end date as YYYY-MM-DD, the day of the week it ends on (use toLocaleDateString with { weekday: "long", timeZone: "UTC" }), and how many days are left counting from 2026-10-24T00:00:00Z.

Show solution
JavaScript
const start = new Date("2026-09-01T00:00:00Z");
const end = new Date(start);
end.setUTCDate(end.getUTCDate() + 75);

const today = new Date("2026-10-24T00:00:00Z");
const msPerDay = 24 * 60 * 60 * 1000;

console.log(end.toISOString().slice(0, 10));
console.log(end.toLocaleDateString("en-IN", { weekday: "long", timeZone: "UTC" }));
console.log(Math.round((end - today) / msPerDay), "days left");
Output
2026-11-15
Sunday
22 days left

Key takeaway#

Finished this lesson?