Moment.js
Legacy date and time library for parsing, formatting, and manipulating dates with a mutable, chainable API.
Official Documentation ↗v2 (maintenance mode). Check the official docs for the latest API surface.Installation
npm install momentpnpm add momentMethods
No methods match this filter.
Overview
Moment.js is a legacy date and time library for parsing, formatting, and manipulating dates with a mutable chainable API. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
moment— Parses the input into a mutable Moment object, defaulting to the current time.format— Renders the date using token-based format patterns.add— Mutates and returns the moment after adding the given duration.
Example
const moment = require('moment');
const d = moment('2026-10-23', 'YYYY-MM-DD');
console.log(d.isValid());
Notes
Moment is in maintenance mode and is not tree-shakeable, so new projects generally prefer date-fns, Luxon, or dayjs. Always pass an explicit format when parsing.
Verification
Parse a date with and without a format string and confirm the explicit format yields isValid() true regardless of environment.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://momentjs.com/docs/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:58.044Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- This hides the warning, but does not make additional locales available. (https://momentjs.com/docs/)
- A matching IgnorePlugin prevents the warning, so ignoreWarnings is not needed. (https://momentjs.com/docs/)
- In order: Prefer formats resulting in valid dates over invalid ones. (https://momentjs.com/docs/)
Security notes
Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://momentjs.com/docs/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:58.044Z.
- Report security vulnerabilities privately through the repository’s security page . (https://momentjs.com/docs/)
- Escaping characters To escape characters in format strings, you can wrap the characters in square brackets. (https://momentjs.com/docs/)
- The Moment project does not maintain them and cannot verify their current suitability, maintenance status, security, or compatibility with Moment.js. (https://momentjs.com/docs/)
FAQ
- Is Moment.js still recommended for new projects?
- Moment is in maintenance mode; new projects are encouraged to use smaller tree-shakeable options such as date-fns, Luxon, or dayjs.
- Why does parsing sometimes produce a wrong date?
- Without an explicit format, Moment falls back to loose parsing that depends on the environment; always pass a format string for reliability.
- How do I handle time zones?
- Install moment-timezone and use moment.tz(..., zone) to parse and render in a specific zone.
Report a problem with this API docOpens a pre-filled issue on GitHub.