Back to Articles
Jan 17, 2022
3 min read

Goodbye Moment, but only if you're headache-free!

MomentJS has been the go-to remedy for developer headaches since 2011. It’s suitable for all ages, weights, and doesn’t require a prescription. But why is this JavaScript library slowly approaching its end after being the best for so long? Let’s find out 😉

Ibuprofen box with MomentJS replacing the drug's name. Image created by Simone Trovato Monastra for the occasion.

This article was originally written for Devmy and can be found here along with many other interesting reads. 😉

What is Moment?

Programming languages offer various remedies for treating the date problem. However, these are simple methods allowing only basic operations. For more complex tasks, it’s better to use fast-acting “medications” like libraries. MomentJS is one such library.

MomentJS is an open-source anti-inflammatory built with JavaScript. Its numerous applications stem from its particular effectiveness in combating the unwieldiness of the native Date object, proving useful not just in date manipulation but also in parsing, validation, formatting, and internationalization. These are precious utilities for safeguarding a developer’s mental health.

Where is it used?

MomentJS, being a Vanilla JavaScript library, can be used both in browsers and Node.js environments. This allows development teams to work with the same technology across the application stack without suffering from the dreaded context switch.

When should you use Moment.js?

It hasn’t been officially banned by any regulatory authority, but in most cases, we shouldn’t choose MomentJS for new projects.

However, there are situations where it’s still a valid option, such as when maintaining backward compatibility with older browsers like Internet Explorer 8 or later, or when using third-party libraries that include Moment as a dependency.

Why shouldn’t I use it anymore?

Moment.js is a great library with fantastic features and utilities. However, it comes with several drawbacks:

  • Tree shaking: Due to its development structure, it doesn’t work well with tree-shaking, leading to large bundle sizes and performance issues.

  • Mutable objects: Contrary to expectations, Moment objects are not immutable. Its fluent methods always return the modified object reference instead of a new instance, causing unexpected aliasing bugs for first-time users.

  • Complexity: Many of Moment’s APIs are unnecessarily complex, sometimes bordering on absurdity in the name of flexibility. For instance, the add and subtract methods allow you to input quantities and units in any order and type, potentially all as strings 😱.

What are the alternatives?

The JavaScript ecosystem offers many modern solutions for date management:

  • INTL (native)
  • Luxon
  • date-fns
  • js-Joda
  • Day.js

Conclusion

There’s no doubt about it: Moment has alleviated developer headaches for years, saving us from the pain of JavaScript’s native Date object. A true hero for all of us.

Moreover, Moment has humbly bid us farewell by listing the reasons why it shouldn’t be used anymore directly on its documentation page, along with recommendations for alternative libraries.

A great library and an exemplary attitude from its developers. Goodbye Moment, and thank you for everything 👋

This is a technical aid in JavaScript. Read the illustrative readme carefully. Do not administer in new projects.

Credits

  • Cover image created by Simone Trovato Monastra ❤️
  • Text revisions by Elena Costa ❤️