JavaScript Academy · Lesson

Formatting Dates

Display dates with toLocaleDateString options.

Lesson 3 of 413 steps

Formatting Dates is a free JavaScript Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

Formatting Dates

JavaScript provides several built-in methods to turn a date into readable text, from locale-aware output to standardized ISO strings.

const d = new Date(Date.UTC(2024, 0, 15))
console.log(d.toISOString())

toISOString

toISOString returns a standardized string in UTC, ideal for storage and data exchange because it is unambiguous.

const d = new Date(Date.UTC(2024, 0, 15, 10, 30, 0))
console.log(d.toISOString())

Slicing the ISO String

The ISO string places the date first, so you can slice off the first ten characters to get just the calendar date.

const d = new Date(Date.UTC(2024, 0, 15))
console.log(d.toISOString().slice(0, 10))

toLocaleDateString

toLocaleDateString formats the date portion using a locale, producing region-appropriate output.

const d = new Date(Date.UTC(2024, 0, 15))
const text = d.toLocaleDateString("en-US", { timeZone: "UTC" })
console.log(typeof text)

Locale Argument

The first argument is a locale tag like en-US or de-DE, which changes the order and separators of the formatted date.

const d = new Date(Date.UTC(2024, 0, 15))
const us = d.toLocaleDateString("en-US", { timeZone: "UTC" })
const iso = d.toISOString().slice(0, 10)
console.log(typeof us, iso)

Formatting Options

An options object lets you choose how each part appears, such as a long month name or a numeric year.

const d = new Date(Date.UTC(2024, 0, 15))
const text = d.toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric", timeZone: "UTC" })
console.log(typeof text)

toLocaleTimeString

toLocaleTimeString formats only the time portion, again respecting the locale and any options you provide.

const d = new Date(Date.UTC(2024, 0, 15, 14, 5, 0))
const text = d.toLocaleTimeString("en-US", { timeZone: "UTC" })
console.log(typeof text)

toLocaleString

toLocaleString combines both date and time into a single localized string.

const d = new Date(Date.UTC(2024, 0, 15, 9, 0, 0))
const text = d.toLocaleString("en-US", { timeZone: "UTC" })
console.log(typeof text)

toDateString

toDateString gives a fixed, human-readable English date without locale options, useful for quick logging.

const d = new Date(Date.UTC(2024, 0, 15))
console.log(typeof d.toDateString())

Intl.DateTimeFormat

For repeated formatting, Intl.DateTimeFormat creates a reusable formatter object, which is more efficient than calling locale methods many times.

const fmt = new Intl.DateTimeFormat("en-US", { timeZone: "UTC", dateStyle: "medium" })
const d = new Date(Date.UTC(2024, 0, 15))
console.log(typeof fmt.format(d))

Choosing a Format

Use ISO strings for storage and APIs, and locale methods for display to users in their region. Keep these concerns separate.

const d = new Date(Date.UTC(2024, 0, 15))
console.log(d.toISOString().slice(0, 10))

Quick Check

Test your formatting knowledge.

Recap

Recap: Pick the right formatting method for the job.

  • toISOString for storage and APIs
  • toLocaleDateString and friends for user display
  • Options control month names and number styles
  • Intl.DateTimeFormat is efficient for repeated formatting
const d = new Date(Date.UTC(2024, 0, 15))
console.log(d.toISOString().slice(0, 10))
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Formatting Dates” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Formatting Dates”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

What will I learn in “Formatting Dates”?

Display dates with toLocaleDateString options. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Formatting Dates” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating and Reading Dates
  2. Timestamps and Date Math
  3. Formatting Dates
  4. Time Zones and UTC
← Back to JavaScript Academy