What is moment?
The moment npm package is a JavaScript library for parsing, validating, manipulating, and formatting dates. It is widely used for handling dates and times in Node.js and browser applications.
What are moment's main functionalities?
Parsing
Parse a date string into a moment object.
moment('2023-04-05')
Validation
Check if a date is valid.
moment('2023-04-05').isValid()
Manipulation
Manipulate dates by adding time to the current date.
moment().add(7, 'days')
Formatting
Format a moment date object into a string in a specified format.
moment().format('YYYY-MM-DD')
Query
Query dates to compare them.
moment().isBefore('2023-12-25')
Other packages similar to moment
date-fns
Provides a collection of simple, pure functions for manipulating JavaScript dates. It is modular, allowing you to pick and choose which functions you need, and is generally considered to be a lighter-weight alternative to moment.
luxon
Created by one of the original Moment.js team members, Luxon is a modern library for working with dates and times. It offers a fluent API and aims to have a smaller footprint than moment.
dayjs
Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for modern browsers with a largely Moment.js-compatible API. It is lightweight and its API is designed to be familiar to Moment.js users.
A lightweight JavaScript date library for parsing, validating, manipulating, and formatting dates.
Documentation
Port to ECMAScript 6 (version 2.10.0)
Moment 2.10.0 does not bring any new features, but the code is now written in
ECMAScript 6 modules and placed inside src/
. Previously moment.js
, locale/*.js
and
test/moment/*.js
, test/locale/*.js
contained the source of the project. Now
the source is in src/
, temporary build (ECMAScript 5) files are placed under
build/umd/
(for running tests during development), and the moment.js
and
locale/*.js
files are updated only on release.
If you want to use a particular revision of the code, make sure to run
grunt transpile update-index
, so moment.js
and locales/*.js
are synced
with src/*
. We might place that in a commit hook in the future.
Upgrading to 2.0.0
There are a number of small backwards incompatible changes with version 2.0.0. See the full descriptions here
-
Changed language ordinal method to return the number + ordinal instead of just the ordinal.
-
Changed two digit year parsing cutoff to match strptime.
-
Removed moment#sod
and moment#eod
in favor of moment#startOf
and moment#endOf
.
-
Removed moment.humanizeDuration()
in favor of moment.duration().humanize()
.
-
Removed the lang data objects from the top level namespace.
-
Duplicate Date
passed to moment()
instead of referencing it.
We're looking for co-maintainers! If you want to become a master of time please
write to ichernev.
In addition to contributing code, you can help to triage issues. This can include reproducing bug reports, or asking for vital information such as version numbers or reproduction instructions. If you would like to start triaging issues, one easy way to get started is to subscribe to moment/moment on CodeTriage.
License
Moment.js is freely distributable under the terms of the MIT license.