Moment.js is a legacy JavaScript date library for parsing, validating, manipulating, and formatting dates.
Parsing Dates
// Parse current timestamp
const now = moment();
// Parse with explicit format string
const m = moment('2026-10-25', 'YYYY-MM-DD');
// Parse Unix timestamp (seconds vs milliseconds)
moment.unix(1730000000); // from seconds
moment(1730000000000); // from milliseconds
// Check validity
m.isValid(); // -> true
Formatting Tokens
m.format(); // "2026-10-25T00:00:00+08:00" (ISO 8601)
m.format('dddd'); // "Sunday"
m.format('MMM Do YY'); // "Oct 25th 26"
m.fromNow(); // "3 hours ago"
m.calendar(); // "Today at 12:00 PM"
Date Tokens Table
| Token | Output Range | Description |
|---|---|---|
YYYY |
2026 |
4-digit year |
YY |
26 |
2-digit year |
MM |
01..12 |
2-digit month |
MMM |
Jan..Dec |
Short month name |
MMMM |
January..December |
Full month name |
DD |
01..31 |
2-digit day of month |
Do |
1st..31st |
Day of month with ordinal |
d |
0..6 |
Day of week (0 is Sunday) |
ddd |
Sun..Sat |
Short day of week |
dddd |
Sunday..Saturday |
Full day of week |
Q |
1..4 |
Quarter of the year |
Time Tokens Table
| Token | Output Range | Description |
|---|---|---|
HH |
00..23 |
24-hour hour (2-digit) |
H |
0..23 |
24-hour hour (1-digit) |
hh |
01..12 |
12-hour hour (2-digit) |
mm |
00..59 |
Minute (2-digit) |
ss |
00..59 |
Second (2-digit) |
SSS |
000..999 |
Milliseconds (3-digit) |
A |
AM / PM |
Uppercase meridiem |
a |
am / pm |
Lowercase meridiem |
Z |
+08:00 |
Difference from UTC as +-HH:mm |
Date Manipulation
Moment objects are mutable. Operations modify the instance directly:
// Adding and subtracting
moment().add(7, 'days');
moment().subtract(1, 'month');
// Setting to boundary of unit
moment().startOf('day'); // 00:00:00.000 today
moment().endOf('month'); // 23:59:59.999 last day of month
// Cloning to avoid unexpected mutation
const original = moment();
const nextWeek = original.clone().add(7, 'days');
Localized Format Presets
| Format | Output Example | Description |
|---|---|---|
LT |
8:30 PM |
Time |
LTS |
8:30:25 PM |
Time with seconds |
L |
10/25/2026 |
Short date |
LL |
October 25, 2026 |
Long date |
LLL |
October 25, 2026 8:30 PM |
Date and time |
LLLL |
Sunday, October 25, 2026 8:30 PM |
Full day, date, and time |
Modern Alternatives
Moment.js is in maintenance mode. Recommended modern replacements:
- date-fns: Immutable, modular tree-shakeable utility functions.
- Day.js: 2KB lightweight drop-in API replacement for Moment.
- Luxon: Modern date-time library by Moment authors, built on
Intl. - Native
Intl.DateTimeFormat: Built into modern browsers and Node.js with zero dependencies.