Switch to infinite scroll (Full book)

Format dates with Luxon

Luxon uses DateTime as its main API to work with dates. We first instantiate a DateTime instance.

(preliminary) instantiate a DateTime instance

We can instantiate a DateTime from:

  • a JS Date
  • an ISO string
  • a milliseconds timestamp
DateTime.fromJSDate(date, { zone: "Europe/London"; });
DateTime.fromISO(isoString, { zone: "Europe/London"; });
DateTime.fromMillis(ms, { zone: "Europe/London"; });

format calendar component(s) with a format string

Date components format strings:

dt.toFormat("yyyy") // "2026"
dt.toFormat("yy") // "26"

dt.toFormat("MMMM") // "January"
dt.toFormat("MMM") // "Jan"
dt.toFormat("MM") // "01"
dt.toFormat("M") // "1"

dt.toFormat("dd") // "09"
dt.toFormat("d") // "9"

dt.toFormat("EEEE") // "Friday"
dt.toFormat("EEE") // "Fri"

Time component format strings:

dt.toFormat("HH:mm:ss") // "08:05:05"

dt.toFormat("H 'hours'") // "8 hours"
dt.toFormat("m 'minutes'") // "5 minutes"
dt.toFormat("s 'seconds'") // "5 seconds"

dt.toFormat("s.SSS's'") // "5.123s"

// 12 hour format
dt.toFormat("h:mm a") // "8:05 AM"

add literal text

We can add literal text safely with single quotes:

dt.toFormat("MMMM d 'at' HH:mm") // "June 19 at 14:30"

Some characters such as slashes can be added unquoted as they don't clash with date and time tokens:

dt.toFormat("yyyy/MM/dd") // "2026/06/19"

ISO formatting

DateTime supports three ISO string helpers:

dt.toISO() // "2026-02-13T19:50:15.123+01:00" <-- Includes offset
dt.toISODate() // "2026-02-13"
dt.toISOTime({ includeOffset: false }) // '07:34:19.361'

(optional) read individual calendar components

We can read the calendar components, as numbers, or sometimes as strings:

const dt = DateTime.fromJSDate(date, { zone: "Europe/London" })

// Getters
dt.zoneName // 'Europe/London'
dt.year
dt.month // 1-12
dt.day // 1-31
dt.hour // 0-23
dt.minute // 0-59
dt.second // 0-59
dt.millisecond

dt.weekdayLong // 'Monday', 'Tuesday', etc.
dt.monthLong // 'January', 'February', etc.

For destructuring, we convert to an object beforehand:

// Make plain object and destructure
const { year, month, day, hour, minute, second } = dt.toObject()
earlymorning logo

Format dates with Luxon

Luxon uses DateTime as its main API to work with dates. We first instantiate a DateTime instance.

(preliminary) instantiate a DateTime instance

We can instantiate a DateTime from:

  • a JS Date
  • an ISO string
  • a milliseconds timestamp
DateTime.fromJSDate(date, { zone: "Europe/London"; });
DateTime.fromISO(isoString, { zone: "Europe/London"; });
DateTime.fromMillis(ms, { zone: "Europe/London"; });

format calendar component(s) with a format string

Date components format strings:

dt.toFormat("yyyy") // "2026"
dt.toFormat("yy") // "26"

dt.toFormat("MMMM") // "January"
dt.toFormat("MMM") // "Jan"
dt.toFormat("MM") // "01"
dt.toFormat("M") // "1"

dt.toFormat("dd") // "09"
dt.toFormat("d") // "9"

dt.toFormat("EEEE") // "Friday"
dt.toFormat("EEE") // "Fri"

Time component format strings:

dt.toFormat("HH:mm:ss") // "08:05:05"

dt.toFormat("H 'hours'") // "8 hours"
dt.toFormat("m 'minutes'") // "5 minutes"
dt.toFormat("s 'seconds'") // "5 seconds"

dt.toFormat("s.SSS's'") // "5.123s"

// 12 hour format
dt.toFormat("h:mm a") // "8:05 AM"

add literal text

We can add literal text safely with single quotes:

dt.toFormat("MMMM d 'at' HH:mm") // "June 19 at 14:30"

Some characters such as slashes can be added unquoted as they don't clash with date and time tokens:

dt.toFormat("yyyy/MM/dd") // "2026/06/19"

ISO formatting

DateTime supports three ISO string helpers:

dt.toISO() // "2026-02-13T19:50:15.123+01:00" <-- Includes offset
dt.toISODate() // "2026-02-13"
dt.toISOTime({ includeOffset: false }) // '07:34:19.361'

(optional) read individual calendar components

We can read the calendar components, as numbers, or sometimes as strings:

const dt = DateTime.fromJSDate(date, { zone: "Europe/London" })

// Getters
dt.zoneName // 'Europe/London'
dt.year
dt.month // 1-12
dt.day // 1-31
dt.hour // 0-23
dt.minute // 0-59
dt.second // 0-59
dt.millisecond

dt.weekdayLong // 'Monday', 'Tuesday', etc.
dt.monthLong // 'January', 'February', etc.

For destructuring, we convert to an object beforehand:

// Make plain object and destructure
const { year, month, day, hour, minute, second } = dt.toObject()