Back to JavaScript Notes
Topic #318

Date Formats

By the end, you can choose a JavaScript date string format that is unambiguous, parse it reliably, and produce either a machine-friendly or human-friendly output.

What it is

A JavaScript Date represents an instant in time as milliseconds since 1970-01-01T00:00:00Z. A date format is the text form used to create or display that instant. The two most important families are ISO 8601, such as 2026-03-15T14:30:00Z, and locale strings, such as March 15, 2026. A Date can also produce strings with toString(), toISOString(), and toLocaleDateString(). Related terms include parsing, formatting, UTC, local time, timezone offset, and invalid date.

Why it matters

  • APIs, databases, and logs usually expect ISO 8601 strings because they are unambiguous.
  • User interfaces need locale-aware strings so dates match the reader's language and conventions.
  • Bad formats cause off-by-one-day bugs when UTC and local time differ.
  • Consistent formats make tests, exports, and data exchange predictable.

Syntax or steps

For storage and transfer, use ISO 8601: YYYY-MM-DDTHH:mm:ss.sssZ. Create a date from an ISO string with new Date(isoString). Produce an ISO string with toISOString(). For display, use toLocaleDateString() or Intl.DateTimeFormat.

const now = new Date();
const iso = now.toISOString();
const parsed = new Date(iso);
const display = parsed.toLocaleDateString("en-US", {
  year: "numeric",
  month: "long",
  day: "numeric"
});

Example

const event = new Date("2026-03-15T14:30:00Z");

console.log(event.toISOString());
console.log(event.toLocaleDateString("en-US"));
console.log(event.toLocaleDateString("fr-FR"));
console.log(event.toLocaleString("en-US", { timeZone: "UTC" }));

const dateOnly = new Date("2026-03-15");
console.log(dateOnly.toISOString());

The first line creates an instant at 14:30 UTC. toISOString() returns the same instant in ISO 8601 format. The locale calls produce human-readable strings for English and French. The final call shows that a date-only ISO string is treated as UTC, so 2026-03-15 becomes 2026-03-15T00:00:00.000Z.

Common mistakes

  • Parsing arbitrary strings. new Date("15/03/2026") may be implementation-dependent. Fix: use ISO 8601 or parse components manually.
  • Forgetting that toISOString() is UTC. A local evening date can become the next day in UTC. Fix: convert to the intended timezone before formatting for users.
  • Using Date.parse() without checking the result. It returns NaN for invalid strings, and toISOString() throws on an invalid Date. Fix: test with Number.isNaN(Date.parse(value)) or Number.isNaN(date.getTime()).
  • Displaying raw ISO strings to users. They are precise but not friendly. Fix: use toLocaleDateString() with explicit options.

When to use it

Choose the format based on the audience. ISO 8601 is for machines; locale strings are for people.

FormatUse whenExample
toISOString()APIs, logs, storage, tests2026-03-15T14:30:00.000Z
toLocaleDateString()UI displayMarch 15, 2026
getTime()Math and comparisons1773585000000

Practice

Guided exercise: Create a Date from 2026-07-04T12:00:00Z, then log its ISO string and a long-form English date.

const d = new Date("2026-07-04T12:00:00Z");
console.log(d.toISOString());
console.log(d.toLocaleDateString("en-US", {
  year: "numeric",
  month: "long",
  day: "numeric"
}));

Expected output: 2026-07-04T12:00:00.000Z and July 4, 2026.

Challenge: Given "2026-12-25T00:00:00", produce an ISO string. Hint: parse it as a Date, then call toISOString(); the result depends on the local time zone.

Quick check

Which JavaScript date string should you send to an API?

Use an ISO 8601 UTC string, usually produced by toISOString().

Summary

JavaScript date strings are either machine formats or human formats. ISO 8601 gives unambiguous instants, while locale formatting gives readable output. Pick the format based on whether the reader is code or a person.

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

Date Formats – FAQs

Quick answers about learning Date Formats in JavaScript.

This free note from Coding Now Tech Institute explains Date Formats 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 Date Formats, is 100% free with no signup required.
With focused practice, most students grasp Date Formats 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