A JavaScript date library for parsing, manipulating, and formatting dates. Use this tag for issues with moment, moment-timezone, and any moment plugins. NOTE: As of September 2020, the Moment team recommends you chose a different library for new projects. See the Project Status section of the Moment docs. On StackOverflow, please refrain from recommending Moment as a solution unless Moment is specifically asked for in the question.
Moment.js is a JavaScript date library for parsing, manipulating, and formatting dates. It is design to work in both the browser and in NodeJS. Moment supports dates in all standard formats, locales, relative time, and time zones.
Important:
As of September 2020, the Moment team recommends you choose a different library than Moment for new projects. Please read https://momentjs.com/docs/#/-project-status/
On StackOverflow, please refrain from recommending Moment as a solution unless Moment is specifically asked for in the question.
Resources
Basic examples
Parsing in a specific format
moment("04/07/2013","MM/DD/YYYY")
Current date in default format
moment().format()
Current date in a custom format
moment().format('MM/DD/YYYY')
Manipulation, calendar time
moment().subtract('days', 3).calendar()
Internationalization
moment.locale('fr');
moment().format('LLLL')
Common Issues
Do not attempt to use the internal fields, which are prefixed with underscores, such as
_i
or_d
. They must be interpreted in a specific way to be useful. Instead, use the public API functions, such as.format()
and others.Don't forget that
moment
objects are mutable. For example:var a = moment('2015-01-01'); var b = moment.year(2000);
Now, both
b
anda
are set to the samemoment
object, having year 2000. For these to be separate objects, you must first clone the moment.var a = moment('2015-01-01'); var b = moment.clone().year(2000);
You can also clone a moment by passing a moment into the moment constructor.
var a = moment('2015-01-01'); var b = moment(a).year(2000);
Remember that like the
Date
object, months are 0-11 when passed numerically, such as with themonth
function, or when passing an array to the moment constructor.
Related Tags
- angular-moment: Moment.js directives for AngularJS
- moment-recur: Moment.js plugin for matching and generating recurring dates