Skip to content

Guide · Open source

How to use Moment.js: install, parse and format dates

Updated · written with AI from the project's own documentation and checked against our catalogue data

Moment.js is a JavaScript library for working with dates and times. It reads date strings, checks whether they are valid, changes them (such as adding days or shifting time zones) and displays them in readable formats. This guide shows you how to install it, what it is good for, and when a newer library is the better choice.

Moment.jsmoment/moment · ★ 47.9k · MIT

Quick start

  1. 1Check your Node.js setup. Confirm Node.js is installed in your environment. The README lists it as a prerequisite but does not state a required version.
  2. 2Open your project folder. Move into the JavaScript project where you want to use date handling.
  3. 3Install Moment.js. Run npm install moment. yarn or bun can also be used, as the README notes.
  4. 4Read the documentation. Open the official docs at momentjs.com/docs to find the functions for parsing, validating and formatting dates.
  5. 5Add date logic to your code. Use the library in your JavaScript files following the documentation examples.
  6. 6Review your long-term choice. If this is a new project, compare Moment.js with date-fns, Day.js, Luxon, and the Temporal API polyfill before committing.

What Moment.js does

Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. It is a code library, so you use it inside a JavaScript program rather than as a standalone app.

The README now marks Moment.js as a legacy project in maintenance mode. New features are not being accepted, so it is mainly suited to keeping existing code working.

  • Parse date strings into date objects
  • Check whether a date is valid
  • Change dates, for example by adding days or shifting time zones
  • Display dates in readable formats

Install Moment.js in your project

Moment.js is installed into a JavaScript project with a package manager such as npm, yarn, or bun. The README gives this npm command. Node.js is the prerequisite listed, but the README does not specify a version.

Run the command from your project folder, where your package.json file is.

npm install moment

Use Moment.js in your code

Once installed, your project can load the library and use its date functions. The README does not include usage examples, so check the official documentation at https://momentjs.com/docs/ for the exact API calls.

The project's documentation is the main reference for parsing, validating, manipulating and formatting dates.

  • Read the official docs at momentjs.com/docs for the API
  • Search Stack Overflow under the momentjs tag for common questions
  • Check the changelog and security policy in the repository before upgrading

Decide whether Moment.js fits your project

Moment.js suits maintaining existing JavaScript projects that already depend on it, and developers who need a familiar date API while not starting something new. Teams whose code already uses Moment can keep it running with bug-level upkeep.

For a new project, the README's note that new features are not accepted is a good reason to look at other options. Comparable projects named in this directory include date-fns, Day.js, Luxon, and the Temporal API polyfill.

  • Good fit: existing projects that already use Moment.js
  • Good fit: developers who want a familiar date API for an existing codebase
  • Poor fit: new projects, where newer libraries may be a better long-term choice
  • Poor fit: anyone who needs a tool with no programming involved

What people use it for

  • Reading date strings that come from a form or an API and checking they are valid
  • Adding or subtracting days and other units from a date in an existing app
  • Shifting dates between time zones in a legacy JavaScript codebase
  • Displaying dates in readable formats for users of an older web application

Frequently asked questions

How do I install Moment.js?

Run npm install moment in your JavaScript project. The README also says you can use yarn or bun as your package manager.

Is Moment.js free to use?

Yes. Moment.js is licensed under the MIT license, and the README says it is freely distributable under those terms.

What do I need to run Moment.js?

You need Node.js, according to the prerequisites listed here. The README does not specify which Node.js version is required.

Is Moment.js still maintained?

The README says it is a legacy project in maintenance mode, and new features are not being accepted. It is still maintained at the bug level for existing users.

What are good alternatives to Moment.js?

Comparable projects listed here are date-fns, Day.js, Luxon, and the Temporal API polyfill. These are worth considering for new work.

Can I use Moment.js without writing code?

No. Moment.js is a code library for JavaScript programs, so it is not suited to people who want a tool with no programming involved.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Use Moment.js if you are maintaining an existing JavaScript project that already depends on it and you want to keep its familiar date API working. For new projects, pick a newer option such as date-fns, Day.js, Luxon, or the Temporal API polyfill, since Moment.js is in maintenance mode and not accepting new features.

Install options & downloads