Getting started

Introduction

Moment.js parses, formats, and manipulates dates. It is a legacy project in maintenance mode: fine for existing code, discouraged for new projects.

Maintenance status

Moment 2.31.0 is the current release. The project adds no new user-facing features, keeps the mutable API, and prioritizes compatibility over bug fixes. The maintainers recommend other libraries for new code.

See: Project status

Moving off Moment

LibraryNotes
Day.js~2 kB, Moment-compatible API
date-fnsFunctions, tree-shakeable, immutable
LuxonIntl zones and locales
TemporalBuilt into the language

Temporal is Stage 4 (ES2026) and ships in Chrome and Edge 144+, Firefox 139+, and Node 26; Safari needs a polyfill.

See: You don't need Moment.js

Core API

Parsing

moment()                                   // now
moment('2013-03-01')                       // ISO 8601
moment('2013-03-01', 'YYYY-MM-DD')         // with a format
moment('2013-03-01', 'YYYY-MM-DD', true)   // strict
moment.unix(1362096000)                    // seconds
moment.utc('2013-03-01T00:00:00Z')         // UTC mode

Pass true for strict parsing of user input. Without a format, non-ISO strings fall back to Date and warn.

See: Parsing

Display

m.format()              // ISO 8601 with offset
m.format('dddd')        // "Friday"
m.format('MMM Do YY')   // "Mar 1st 13"
m.fromNow()             // "7 years ago"
m.calendar()            // "03/01/2013"
m.diff(other, 'days')   // whole days
m.unix()                // seconds
m.valueOf()             // milliseconds

diff() truncates toward zero; add true as a third argument for a float.

See: Format

Manipulating

m.add(1, 'day')
m.subtract(2, 'days')
m.startOf('day')
m.endOf('month')

Moment objects are mutable: add() and startOf() return the same instance. Call m.clone() before branching.

See: Manipulating

Querying

m.isValid()
m.isBefore(other)
m.isAfter(other)
m.isSame(other, 'day')
m.isBetween(from, to, 'day')
m.isSameOrBefore(other)

isSame() and isBetween() take a unit to pin the comparison to that granularity.

See: Query

Durations

const d = moment.duration(2, 'days')
d.asHours()                                  // 48
d.humanize()                                 // "2 days"
moment.duration(90, 'minutes').humanize()    // "an hour"

Durations convert units, and can be passed to m.add(d) or m.subtract(d).

See: Durations

Internationalization

Locale formats

.format('L')      // 06/09/2014
.format('l')      // 6/9/2014
.format('LL')     // June 9 2014
.format('ll')     // Jun 9 2014
.format('LLL')    // June 9 2014 9:32 PM
.format('lll')    // Jun 9 2014 9:32 PM
.format('LLLL')   // Monday, June 9 2014 9:32 PM
.format('llll')   // Mon, Jun 9 2014 9:32 PM

The L tokens follow the active locale, so date order adapts to each language.

See: Format

Locales

import 'moment/locale/fr'   // registers French
moment.locale('fr')         // switches globally
m.locale('de')              // switches one instance

Locales are not bundled by default in Node; import each one you need. Bundlers include all of them unless filtered.

See: i18n

Time zones

import moment from 'moment-timezone'

moment.tz('2024-06-01 12:00', 'America/New_York')
moment.tz.guess()
moment.utc().tz('Europe/Paris')

Time zones live in the separate moment-timezone package, which bundles IANA data.

See: Moment Timezone

Formatting

Examples

Date

ExampleOutput
YYYY-MM-DD2014-01-01
dddd, MMMM Do YYYYFriday, May 16th 2014
dddd [the] Do [of] MMMMFriday the 16th of May

Time

ExampleOutput
hh:mm a12:30 pm

Moment.js tokens; date-fns and Java SimpleDateFormat use different ones. Moment itself is in maintenance mode.

See: Format

Date

SymbolExampleArea
d0..6Weekday
ddSu 
dddSun 
ddddSunday 
YY13Year
YYYY2013 
M1..12 (Jan is 1)Month
Mo1st..12th 
MM01..12 (Jan is 1) 
MMMJan 
MMMMJanuary 
Q1..4Quarter
Qo1st..4th 
D1..31Day
Do1st..31st 
DD01..31 
DDD1..365Day of year
DDDo1st..365th 
DDDD001..365 
w1..53Week of year
wo1st..53rd 
ww01..53 

See: Format

Time

SymbolExampleArea
H0..2324h hour
HH00..23 
h1..1212h hour
hh01..12 
m0..59Minutes
mm00..59 
s0..59Seconds
ss00..59 
aamAM/PM
AAM 
Z+07:00Zone offset
ZZ+0730 
S0..9Deciseconds
SS00..99Centiseconds
SSS000..999Milliseconds
X1360013296Unix timestamp
x1360013296123Unix ms timestamp

See: Format

Presets

ExampleOutput
LT8:30 PM
LTS8:30:25 PM
LLAugust 2, 1985
llAug 2, 1985
LLLAugust 2, 1985 8:30 PM
lllAug 2, 1985 8:30 PM
LLLLFriday, August 2, 1985 8:30 PM
llllFri, Aug 2, 1985 8:30 PM

See: Long date formats

References

0 Comments for this cheatsheet. Write yours!