Getting started
Introduction
Luxon is a date and time library built on Intl. This reference covers parsing, formatting, arithmetic, time zones, intervals and durations.
- Luxon documentation (moment.github.io)
- API reference (moment.github.io)
- GitHub repository (github.com)
Create a DateTime
import { DateTime } from 'luxon'
DateTime.now()
DateTime.fromISO('2014-06-09T09:32:00Z')
DateTime.fromObject({ year: 2014, month: 6, day: 9 })
DateTime.fromFormat('09-06-2014', 'dd-MM-yyyy')
DateTime.fromMillis(1402306320000)
Every factory returns an immutable DateTime that carries both an instant and a zone. With no zone option, the local zone applies.
See: Creating DateTimes
Invalid DateTimes
const bad = DateTime.fromISO('2014-13-01')
bad.isValid // false
bad.invalidReason // "unit out of range"
Luxon marks a bad value invalid instead of rolling it into a nearby date. Check isValid before formatting or storing.
See: Validity
Chain operations
const start = DateTime.fromISO('2014-06-09', { zone: 'utc' })
const next = start.plus({ days: 1 })
start.toISODate() // "2014-06-09"
next.toISODate() // "2014-06-10"
No method mutates in place; assign the result or chain from it.
See: Math
Tokens
Date tokens
| Token | Output | Description |
|---|---|---|
yyyy | 2014 | Year, four digits |
yy | 14 | Year, two digits |
LLLL | June | Month name, full |
LLL | Jun | Month name, short |
MM | 06 | Month, two digits |
M | 6 | Month, no leading zero |
dd | 09 | Day of month, two digits |
d | 9 | Day of month |
cccc | Monday | Weekday name, full |
ccc | Mon | Weekday name, short |
Tokens are case-sensitive: yyyy is the year and YYYY prints literally.
See: Formatting
Time tokens
| Token | Output | Description |
|---|---|---|
HH | 09 | Hour, 24-hour, two digits |
H | 9 | Hour, 24-hour |
hh | 09 | Hour, 12-hour, two digits |
h | 9 | Hour, 12-hour |
mm | 32 | Minute, two digits |
ss | 05 | Second, two digits |
SSS | 123 | Millisecond, three digits |
a | AM | Meridiem |
See: Formatting
Zone and epoch tokens
| Token | Output | Description |
|---|---|---|
ZZZZ | UTC | Zone name, short |
ZZ | +00:00 | Offset, with colon |
Z | +0 | Offset, narrow |
X | 1402306325 | Unix timestamp, seconds |
x | 1402306325123 | Unix timestamp, milliseconds |
The outputs assume a UTC instance.
See: Formatting
Formatting
Locale presets
const d = DateTime.fromISO('2014-06-09T09:32:05Z', { zone: 'utc' })
d.toLocaleString(DateTime.DATE_FULL)
// "June 9, 2014"
d.toLocaleString(DateTime.DATETIME_MED)
// "Jun 9, 2014, 9:32 AM"
d.toLocaleString(DateTime.TIME_24_SIMPLE)
// "09:32"
Presets render through Intl, so they adapt to the locale and to the runtime's ICU data.
See: Locale strings
Custom locale
const d = DateTime.fromISO('2014-06-09T09:32:05Z', { zone: 'utc' })
d.setLocale('fr').toLocaleString(DateTime.DATE_FULL)
// "9 juin 2014"
d.setLocale('fr').toFormat('cccc d LLLL yyyy')
// "lundi 9 juin 2014"
setLocale() affects only that instance's output, not the global default.
See: Locales
Escaping text
DateTime.fromISO('2014-06-09').toFormat("'week' W")
// "week 24"
Single quotes protect literal words from being read as tokens.
See: Escaping
ISO output
const d = DateTime.fromISO('2014-06-09T09:32:05.123Z',
{ zone: 'Asia/Tokyo' })
d.toISO() // "2014-06-09T18:32:05.123+09:00"
d.toUTC().toISO() // "2014-06-09T09:32:05.123Z"
d.toISODate() // "2014-06-09"
d.toISOWeekDate() // "2014-W24-1"
toISO() keeps the current zone; toUTC() is what adds the trailing Z.
See: ISO
Relative time
Exact
const d = DateTime.fromISO('2014-06-09T09:32Z', { zone: 'utc' })
const base = DateTime.fromISO('2014-06-19T09:32Z', { zone: 'utc' })
d.toRelative({ base }) // "10 days ago"
Calendar
DateTime.fromISO('2026-11-24T10:00', { zone: 'utc' })
.toRelativeCalendar({
base: DateTime.fromISO('2026-11-26T10:00', { zone: 'utc' })
})
// "2 days ago"
toRelative() counts elapsed time; toRelativeCalendar() names the calendar day. Both need Intl.RelativeTimeFormat.
See: Relative time
Math
Add and subtract
Days and months
import { DateTime, Duration } from 'luxon'
const d = DateTime.fromISO('2014-06-09T09:32', { zone: 'utc' })
d.plus({ days: 3 }).toISODate() // "2014-06-12"
d.minus({ months: 1 }).toISODate() // "2014-05-09"
d.plus(Duration.fromObject({ hours: 2 })).toISO()
// "2014-06-09T11:32:00.000Z"
Month ends
DateTime.fromISO('2014-01-31', { zone: 'utc' })
.plus({ months: 1 }).toISODate() // "2014-02-28"
Plain objects and Duration both work. Months clamp to the last valid day instead of spilling into the next month.
See: Math
DST changes calendar days
const d = DateTime.fromISO('2026-03-07T09:00',
{ zone: 'America/New_York' })
d.plus({ days: 1 }).toISO()
// "2026-03-08T09:00:00.000-04:00"
d.plus({ hours: 24 }).toISO()
// "2026-03-08T10:00:00.000-04:00"
days keep the wall clock across a DST change; hours are exact elapsed time.
See: DST and math
Start and end of unit
const d = DateTime.fromISO('2014-06-11T09:32', { zone: 'utc' })
d.startOf('day').toISO()
// "2014-06-11T00:00:00.000Z"
d.endOf('month').toISO()
// "2014-06-30T23:59:59.999Z"
d.startOf('week').toISODate() // "2014-06-09"
startOf('week') follows ISO weeks and begins on Monday. endOf() lands on the last millisecond.
See: Start and end
Set fields
const d = DateTime.fromISO('2014-06-11T09:32', { zone: 'utc' })
d.set({ month: 1, day: 15 }).toISODate() // "2014-01-15"
d.set({ hour: 0, minute: 0 }).toISO()
// "2014-06-11T00:00:00.000Z"
set() replaces fields; use plus() or minus() to move by an amount.
See: Set
Differences
Fractional
const start = DateTime.fromISO('2014-06-09T00:00', { zone: 'utc' })
const end = DateTime.fromISO('2014-06-10T12:00', { zone: 'utc' })
end.diff(start, 'days').days // 1.5
end.diff(start, 'hours').hours // 36
Breakdown
DateTime.fromISO('2014-06-19T12:30', { zone: 'utc' })
.diff(DateTime.fromISO('2014-06-09T09:32', { zone: 'utc' }),
['days', 'hours', 'minutes'])
.toObject()
// { days: 10, hours: 2, minutes: 58 }
diff() returns a Duration. A single unit gives a fractional count; an array gives whole calendar units.
See: Diff
Time zones
Parse in a zone
DateTime.fromISO('2026-03-08T09:00',
{ zone: 'America/New_York' }).toISO()
// "2026-03-08T09:00:00.000-04:00"
The zone option reads a naive string as wall time in that zone. It also applies to fromObject() and fromFormat().
See: Zones
Convert to a zone
const d = DateTime.fromISO('2014-06-09T09:32:00Z')
d.setZone('America/New_York').toISO()
// "2014-06-09T05:32:00.000-04:00"
d.toUTC().toISO() // "2014-06-09T09:32:00.000Z"
d.toLocal().toISO() // system zone
setZone() keeps the instant and changes the clock; do not use set() for this.
See: Changing zones
Keep the wall clock
const d = DateTime.fromISO('2014-06-09T09:32:00Z')
d.setZone('America/New_York', { keepLocalTime: true }).toISO()
// "2014-06-09T09:32:00.000-04:00"
keepLocalTime reinterprets the clock time in the new zone, which suits reminders and wall-clock schedules.
See: keepLocalTime
Default zone
import { Settings } from 'luxon'
Settings.defaultZone = 'Asia/Tokyo'
DateTime.fromISO('2026-06-01T12:00').toISO()
// "2026-06-01T12:00:00.000+09:00"
Settings.defaultZone replaces the system zone for new DateTimes. Use Settings.now to freeze the clock in tests.
See: Settings
Zone info
const ny = DateTime.fromISO('2026-03-08T09:00',
{ zone: 'America/New_York' })
ny.zoneName // "America/New_York"
ny.offset // -240
ny.isInDST // true
ny.offsetNameShort // "EDT"
Offsets are minutes east of UTC, so -240 means UTC−4.
See: Zone API
Fixed offsets
DateTime.fromISO('2026-06-01T12:00', { zone: 'UTC+5:30' })
.toISO()
// "2026-06-01T12:00:00.000+05:30"
A fixed offset never follows DST. Prefer an IANA zone for real places.
See: Fixed offset zones
Intervals
Create an interval
import { DateTime, Interval } from 'luxon'
const start = DateTime.fromISO('2026-01-01T09:00',
{ zone: 'America/New_York' })
const end = DateTime.fromISO('2026-01-15T17:30',
{ zone: 'America/New_York' })
const range = Interval.fromDateTimes(start, end)
range.toISODate() // "2026-01-01/2026-01-15"
An interval is an ordered pair of DateTimes. fromDateTimes() returns an invalid interval when the end is before the start.
See: Intervals
Measure an interval
range.length('hours') // 344.5
range.count('days') // 15
range.toDuration(['days', 'hours', 'minutes']).toObject()
// { days: 14, hours: 8, minutes: 30 }
length() is exact elapsed time; count() counts calendar units the interval touches.
See: Interval length
Split into weeks
range.splitBy({ days: 7 })
.map((part) => part.toISODate())
// ["2026-01-01/2026-01-08",
// "2026-01-08/2026-01-15",
// "2026-01-15/2026-01-15"]
splitBy() fills the range with evenly sized pieces. The final slice can be shorter than the step, and toISODate() hides its time when it lands on the same date.
See: Split
Contains
range.contains(DateTime.fromISO('2026-01-10T00:00',
{ zone: 'America/New_York' })) // true
range.contains(end) // false
range.contains(end.minus({ seconds: 1 })) // true
The end is exclusive; test the last instant with minus().
See: Contains
Durations
Create a duration
import { Duration } from 'luxon'
Duration.fromObject({ hours: 2, minutes: 30 }).toISO()
// "PT2H30M"
Duration.fromISO('PT1H30M').as('minutes') // 90
Duration.fromMillis(5400000).toFormat('hh:mm') // "01:30"
A duration is a bag of units with no zone. It records the units you gave it, not a span of time.
See: Durations
Format a duration
Duration.fromObject({ days: 2, hours: 3, minutes: 4 })
.toFormat("d'd' h'h' m'm'") // "2d 3h 4m"
Quote literal letters so they are not read as units.
See: Duration format
Human readable
Duration.fromObject({ hours: 2, minutes: 30 }).toHuman()
// "2 hours, 30 minutes"
Duration.fromObject({ hours: 2, minutes: 30 })
.toHuman({ listStyle: 'long' })
// "2 hours and 30 minutes"
toHuman() formats each unit with Intl.NumberFormat and joins them with Intl.ListFormat.
See: Duration human
Convert units
Duration.fromObject({ hours: 25 }).as('days')
// 1.0416666666666667
Duration.fromObject({ minutes: 150 })
.shiftTo('hours', 'minutes').toObject()
// { hours: 2, minutes: 30 }
as() returns a fraction; shiftTo() moves whole units into the units you name.
See: Duration units
Locales and info
Locale data
import { Info } from 'luxon'
Info.months('short')[0] // "Jan"
Info.months('long', { locale: 'fr' })[0] // "janvier"
Info.weekdays('long', { locale: 'fr' })[0] // "lundi"
Info reads names from Intl instead of bundling locale tables.
See: Info
Week numbers
const d = DateTime.fromISO('2026-11-26', { zone: 'utc' })
.setLocale('en-US')
d.weekNumber // 48 (ISO)
d.localWeekNumber // 47 (US)
Info.getStartOfWeek({ locale: 'en-US' }) // 7
Info.getStartOfWeek({ locale: 'fr' }) // 1
ISO weeks start on Monday; the locale week can start on Sunday, which shifts the week number.
See: Week numbers
DST support
Info.hasDST('America/New_York') // true
Info.hasDST('Asia/Tokyo') // false
hasDST() reports whether a zone uses daylight saving at all.
See: Info API
References
- Luxon documentation (moment.github.io)
- API reference (moment.github.io)
- GitHub repository (github.com)
- Day.js cheatsheet (devhints.io)
- Moment.js cheatsheet (devhints.io)
0 Comments for this cheatsheet. Write yours!