avatarJakub Kozak

Summary

The article discusses modern alternatives to Moment.js for date and time manipulation in JavaScript, comparing their features, popularity, and performance.

Abstract

The web content titled "Best Moment.JS Alternatives" provides an in-depth comparison of modern date and time libraries for JavaScript, highlighting the shortcomings of the once-popular Moment.js. It introduces Luxon, Day.js, date-fns, and js-joda as contemporary alternatives, evaluating them based on GitHub stars, NPM package usage, public GitHub repositories, immutability, TypeScript support, performance, and size. The article includes code examples for each library and offers guidance on migrating from Moment.js to a new library. It concludes by inviting feedback on library choices and provides additional resources, including JavaScript learning materials and job opportunities.

Opinions

  • The Moment.js team acknowledges the library as a legacy project and discourages its use in new projects due to its non-immutable API, large size, and lack of tree shaking support.
  • The author recommends creating a date-time service to encapsulate the chosen library, facilitating future changes without extensive codebase updates.
  • The performance tests, which run common operations 100,000 times, suggest that none of the libraries outperforms the others in all categories, but all complete operations in under 3 seconds, indicating adequate performance for most use cases.
  • The author encourages readers to engage with the community by commenting on their chosen library and considering additional factors when making their selection.
  • The article promotes the author's GitHub repository containing code examples and performance tests, as well as membership in Medium and a specific job opportunity at Tjekvik.
  • A personal endorsement is made for an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), with a special offer presented to the readers.

Best Moment.JS Alternatives

Comparing size, performance, Typescript support and more

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.

“We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.” — The Moment team

Modern alternatives

There are multiple solid date and time libraries for JavaScript you can use instead. We’ll look into Luxon, Day.js, date-fns and js-joda.

The following comparison includes attributes that should help you choose the right one for your project.

GitHub stars

GitHub stars

NPM packages using the libraries

NPM packages

Public GitHub repositories using the libraries

Public GitHub repositories

Immutability

Immutability

TypeScript support

TypeScript support

Performance

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

  • ParsingparseISO
  • FormattingYYYY-MM-DD
  • Transformation — add 2 years
  • Differencediff in months between 2 instances
  • Comparingis 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.

Size

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.

Code examples

The entire source code: https://github.com/pixochi/moment-js-alternatives

Luxon

Day.js

date-fns

js-joda

How to migrate from Moment.js

  1. 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.
  2. Create extensive unit tests for the service.
  3. Use eslint-plugin-you-dont-need-momentjs to find all instances of Moment in your codebase and replace them using your new service.
  4. Uninstall moment.js

Conclusion

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:

If you like this article, follow me for more future tips on development with React, JavaScript, TypeScript and more :)

Best JavaScript Books and Courses in 2023

The Complete JavaScript Course 2023: From Zero to Expert

Modern JavaScript From The Beginning 2.0–2023 Revamp

JavaScript from Beginner to Professional: Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages

Become a member

If you enjoy reading stories like these and want to support me as a writer, consider signing up to become a Medium member. It’s $5 a month, giving you unlimited access to stories on Medium. If you sign up using my link, I’ll earn a small commission 🙌

Job Opportunity — Come Join Me

My amazing team at Tjekvik is looking for more developers! Do you have experience with React and Ruby on Rails? Then don’t hesitate and apply right here: https://www.tjekvik.com/careers. You can work from anywhere in Europe! 🌍

Disclosure: Some links on this site are affiliate links. This means I may earn a commission if you make a purchase through those links, at no extra cost to you.

JavaScript
Typescript
Programming
Web Development
Reactjs
Recommended from ReadMedium