Passing Moment, Or Moment.js Replacement

2023-03-04
Javascript
Moment.js
programming
web development

The problem

At some point while dealing with date/time in JS code and searching for some clues, I stumbled over an advice "just use Moment" - and at that time, it was quite revelatory - I wasn't much used to the npm "package for everything" attitude, and haven't really thought that so many problems could have been solved by just using the right library.

And Moment.js stuck with me ever since - not that I needed day/time parsing or manipulation every day, but when I did need it, the answer was clear - just use Moment!

So it was quite a surprise to me that it's being deprecated - in fact, has been for quite some time already. The obvious questions were "why?" and "what now?".

Why?

Apparently Model.js has aged and doesn't do as good a job as it did before - as the maintainers say at the https://momentjs.com/docs/#/-project-status/future/,

Moment was built for the previous era of the JavaScript ecosystem. The modern web looks much different these days. Moment has evolved somewhat over the years, but it has essentially the same design as it did when it was created in 2011.

Some of the problems one could fin on the Web is slowness, large size, mutability and debugging problems. For instance, there's a good comparison of the Moment against other libraries (and native solution) at https://inventi.studio/en/blog/why-you-shouldnt-use-moment-js - it's actually from pre-deprecation times, but I'd expect it still holds true:

Moment.js performance comparison

What now?

There's a "recommendations" section on the Model's website itself: https://momentjs.com/docs/#/-project-status/recommendations/, which list several alternatives, such as:

It's almost too much choice, but luckily that pre-deprecation article I've mentioned earlier has als a comparison of these alternatives considering certain functionality and size:

Date/time libraries comparison

Which (among some suggestions elsewhere) points out that for general cases, Day.js or date-fns are probably the best choice, while for tricky cases and error-prone data Luxon might be a better choice.