Best Moment.JS Alternatives
Moment.js is indeed done
It has been a popular library for parsing, validating, manipulating, and formatting dates for a decade. However, Moment.js has too many drawbacks compared to modern date and time libraries. Its API is not immutable, it is large and it doesn’t support tree shaking. Even the Moment team discourages to use their library in new projects.
The following comparison includes attributes that should help you choose the right one for your project.
NPM packages using the libraries
Public GitHub repositories using the libraries
The following charts show one common operation from each type of date and time manipulation. Each operation in the chart was run 100 000 times.
Types of date and time manipulation
- Parsing — parseISO
- Formatting — YYYY-MM-DD
- Transformation — add 2 years
- Difference — diff in months between 2 instances
- Comparing — is one instance before another instance in time?
From the chart above, you can see that there’s no library that is best for all the operations. Another significant result shows that even 100 000 runs take less than 3 seconds. If you don’t need to process some big data, you shouldn’t be worried about performance of the libraries.
Each size measurement includes the library, its companion library for time zone support if there’s one and polyfills. The polyfills are required for libraries that use the Intl API for locales, time zones, or both.
The entire source code: https://github.com/pixochi/moment-js-alternatives
How to migrate from Moment.js
- Create a date-time service that hides what library you’re using. If you decide to replace the library in the future, you’ll need to update only one file.
- Create extensive unit tests for the service.
- Use eslint-plugin-you-dont-need-momentjs to find all instances of Moment in your codebase and replace them using your new service.
- Uninstall moment.js
I hope the comparison answered your questions about the modern date and time libraries that can replace Moment.js.
Let me and other developers know which library you’ve chosen in the comments. Did you consider any other libraries or additional attributes when choosing the right library?
You can find the entire code examples and performance testing in my GitHub repo:
Compares modern date and time libraries that can be used to replace Moment.js. Includes code examples for Luxon…
You may like
How to Publish Content With the Instagram Graph API
Share Instagram posts from a React app — Step by step guide
How to Build a Chrome Extension with React, TypeScript and Webpack
From creating a boilerplate to publishing a complete extension to Chrome Web Store