Moment.js and its maintenance mode entry
Tags: javascript
Published: 2020-09-21 11:27:20


A couple of days ago, The moment.js team announced the project's [entry to maintenance mode](https://momentjs.com/docs/#/-project-status/). Among the reasons they decided to make this transition is:

> However, 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. Given how many projects depend on it, we choose to prioritize stability over new features.

The option to preserve stability and compatibility over new features in very old and widely used projects is not new. Indeed, The [request](https://github.com/request/request/issues/3142) NPM module made a similar transition several months prior.

Once a project is released, there may come a point in its lifecycle when it gets widely adopted. Consequentially, the devs also get an influx of issues and also some pull requests. They work around the clock to address them and generally do a good job at that.

The same energy cannot be attributed to end users. When developers roll out new features, a new version or depreciate something, for many users there is little to no incentive to keep their own projects' use of the dependency library in sync with best practices, As a result, there is a huge trail of projects using its legacy or old APIs, some relying on old quarks or the effect of long-standing bugs in the dependency, still hosted on package managers and on Github.

This effectively prevents the library developers from rolling out breaking changes.

As more features are added, the legacy behavior must be kept floated, and eventually the codebase becomes so flimsy that a time comes when it *has* to be frozen. Keep in mind though, it's not the same as withdrawing the dependency library, because that would break a lot of projects and stir quite a bit of chaos for users of downstream projects.

To be fair, Moment.js is a big library, and even Lighthouse in Chrome shows that there are other date and time libraries three times smaller than it:

![Moment.js on Chrome Lighthouse](https://pbs.twimg.com/media/EhM0XE3UwAA2Co5?format=jpg&name=4096x4096)

The large size is because it provides a lot more flexibility in creating dates than the `Date()` constructor in Javascript and also because of the sheer number of locales and timezones bundled. Most applications probably don't need to display time in all of them, and especially for Javacript that's bundled for web, you save a lot on your CDN's bandwidth by shedding a few dozen kilobytes sent to each user. Moment.js did all of this while providing a simple way to create them. Here are two snippets of code that both return `3 PM` (assuming the time right now is 3:00 PM). The first uses Moment, the other uses the native Date object in JS.

(The second snippets in each pair were pasted from https://dockyard.com/blog/2020/02/14/you-probably-don-t-need-moment-js-anymore)

```js
// Moment.js
moment().format('h A')
// 3 PM
```

```js
// Native Date object
Intl.DateTimeFormat('en', { hour: 'numeric' }).format(new Date())
// 3 PM
```

As you can see, the first snippet is much shorter to write. Another example that explicitly demonstrates the complications of native Date objects is getting the timezone name along with the hour and AM/PM. Let us assume the timezone is **PST**:

```js
// Moment.js, requires moment-timezone
var a = moment.tz("America/Los_Angeles");
a.format('h A z'); // 3 PM PST
```

```js
// Native Date object
let [, tzName] = /.*\s(.+)/.exec((new Date()).toLocaleDateString(navigator.language, { timeZoneName: 'short' }));
let date = Intl.DateTimeFormat('en', { hour: 'numeric' }).format(new Date())
`${date} ${tzName}` // 3 PM PST
```

The first snippet is also easier to write, and doesn't involve regexes.

So, should you migrate off of Moment? If you don't have a pressing need to, like saving bandwidth, then don't. You are better off using what already works for you. As I always say, don't migrate just for the sake of it. Moment isn't going to go away, it's just not going to get any newer features.

Over the next couple weeks I will look at the possible MomentJS alternatives and discuss them here, and see how they stack up against Moment.

## And we're done

Thanks for reading. If you see any errors in this post, please let me know so I can correct them.