Moment.js
The original JavaScript date library — now officially in maintenance mode.
What it is
Moment.js is a widely-used JavaScript library for parsing, validating, manipulating, and formatting dates and times. It simplifies working with dates across different browsers and time zones.
Moment.js allows you to create, parse, manipulate, and format dates and times easily. You can chain multiple operations, handle relative times, perform comparisons, and work with time zones using Moment Timezone.
- Status
- Maintenance mode — no new features
- Licence
- MIT
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Maintaining an existing codebase that already depends on it
Look elsewhere when
- Any new project — the maintainers themselves recommend against it
- Bundle size matters; Moment is large and not tree-shakeable
- You need immutability — Moment objects are mutable, which causes real bugs
Installation
npm install momentGetting started
The smallest useful thing you can do with it, and what each part means.
import moment from 'moment';
const now = moment();
console.log(now.format('YYYY-MM-DD HH:mm:ss'));const date = moment('2025-08-22', 'YYYY-MM-DD');
console.log(date.format('MMMM Do, YYYY'));Advanced usage
Where the library earns its place over a simpler alternative.
let future = moment().add(7, 'days').subtract(2, 'hours');
console.log(future.format());const date1 = moment('2025-08-22');
const date2 = moment('2025-09-01');
console.log(date1.isBefore(date2)); // true
console.log(date1.isAfter(date2)); // falseconst past = moment('2025-08-15');
console.log(past.fromNow());import 'moment-timezone';
const nyTime = moment.tz('2025-08-22 12:00', 'America/New_York');
console.log(nyTime.format());Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Invalid date
- Ensure that the input string matches the expected format or use Moment's strict parsing mode.
- Unexpected behavior across browsers
- Use Moment.js instead of relying solely on the native Date object to handle inconsistencies.
Best practices
- Use `.clone()` before modifying dates to avoid mutating original Moment objects.
- Prefer ISO 8601 strings for parsing to ensure consistency.
- Use Moment Timezone when working with different time zones.
- Use relative time functions for user-friendly date displays.
- Be aware that Moment.js is in maintenance mode; consider alternatives like date-fns or Luxon for new projects.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Moment.js was created to provide a robust, reliable, and easy-to-use API for date manipulation in JavaScript. It became popular due to inconsistencies in the native Date object and its lack of convenient formatting, parsing, and timezone handling capabilities.
