Should You Really Switch to Day.js From Moment.js?

In recent years, many frontend developers began to look at alternatives for Moment.js, one of the most well-known and most used JavaScript libraries. The reason for the transition is simple: despite its rich functionality, Moment.js has several issues that significantly impact the app’s performance. And today, the most popular alternative to Moment.js is Day.js which surpassed date-fns on GitHub by stars (46,2k Day.js stars vs 37,7k date-fns stars).

So what’s with Day.js and can it really replace Moment.js in your project? Let’s try figuring it out.

Should You Really Switch to Day.js From Moment.js?

Moment.js overview

Moment.js is an open-source time and date JavaScript library. It’s used for manipulating, parsing, validating, and displaying time and data in a human-readable format. A great thing about Moment.js is that it works both in the browser (installation via the script method) and in Node.js (installation via npm). And being the most popular time and date library, Moment.js supports all standard date formats, all standard time zones, relative time, and locales.

So what exactly does Moment.js do? Its biggest features are:

  • Parsing: available in string, object, or array.
  • Validation: you can check whether the date is valid or not by using the isValid() method.
  • Manipulation: Moment.js offers multiple methods for data manipulation, such as startoftime and endoftime, subtract, add, local, etc.
  • Display: you can display a date in different formats needed.
  • Internationalization: date and time are displayed based on the locale. 
  • Customization: you can customize created locales, like changing their abbreviations or customizing months’ names.

Moment.js pros and cons

As you can see from its features, Moment.js allows you advanced date and time manipulation and is overall a convenient and efficient tool. But since we’ll compare it with the Day.js library, it’s important to look at Moment.js pros and cons in order to better understand the reasons behind its popularity and downfall.

Moment.js pros:

  • Rich and simple APIs;
  • Multi-platform compatibility;
  • Advanced date/time formatting;
  • Display of localized dates.

Moment.js cons:

  • Does not work with tree-shaking: results in huge bundle size and causes performance issues.
  • Heavy: its minified weight is 67,9Kb.
  • Has a mutability issue and causes bugs.
  • Has a complex OOP API.

As you can see, there are certain flaws in the otherwise great functionality of Moment.js. Now, let’s look at its alternative aka Day.js, and see what’s so special about it.

Day.js overview

There is not much to say about Day.js after discussing Moment.js since these two libraries are really similar. Day.js was created as a lightweight alternative to Moment.js but apart from its small size (2kb zipped), Day.js has almost the same functionality. It also works in the browser and in Node.js (the installation methods are the same as ones of Moment.js) and it has pretty good APIs. 

In general, Day.js is minimalistic, efficient, and high-performing. And one more advantage: if you already worked with Moment.js, you’ll easily understand Day.js so the learning curve is not steep at all. As for the features, Day.js shares the same ones with Moment.js: parsing, validation, manipulation, customization, etc. 

Day.js pros and cons

So is Day.js completely perfect or does it have any hidden rocks? Spoiler: it does.

Day.js pros:

  • High speed of performance.
  • Small size: only 2kb zipped.
  • Extensive data manipulation.
  • Can be included as a JS script from a CDN or a local file.
  • Immutable: helps prevent bugs and avoid lengthy debugging.
  • Internationalization: Day.js offers support for I18n.

Day.js cons:

  • Is less feature-rich than Moment.js 

So should you replace Moment.js with Day.js?

Overall, yes, you should if you want better app performance and better security. Below, our frontend developer Alexander Gvozd, shares his opinion and explains why the transition is needed.

Reasons why MomentJS is (almost) dead

“I have been working extensively with both libraries and, starting from 2020, I decided to completely replace MomentJS with DayJS on all the projects that I develop and support. There are many reasons for this, and one of the most significant ones is the big size of the MomentJS library. In comparison, the size of DayJS is about 2kb vs 60kb for MomentJS. Think about it: MomentJS weights 30 times more! And if my own experience is not enough, think about the following: even Chrome Dev Tools started showing recommendations for replacing MomentJS because of its size.

The second reason is the need for constant updates of the tools that developers use in their work. The goal of any software developer is to create easy and fast apps and we always strive for maximal optimization. We need to constantly update our libraries and frameworks to the latest versions to ensure the good performance of software products and to maintain their security. What does it have to do with MomentJS? Let me explain.

In 2020, the MomentJS development team announced the end of development and the transition of the project to the maintenance mode. That means, the team stopped the expansion of functionality, froze the API, and limited its activity to fixing critical errors. This is another important reason why we need to switch to DayJS. 

In addition, there are also such issues with MomentJS as its low performance, complex API, problematic debugging, no support for “tree shaking” (cutting unused code to reduce the size of a web application), and obviously the end of support for the library.

Reasons why DayJS is loved by the JS community

First, DayJS now has almost the same methods for working with dates as MomentJS. For example, there was an issue with the lack of support for internationalization in DayJS. The issue is fixed now and DayJS supports time zone via the Internationalization API in supported environments. 

Second, by using a native API, no extra bytes of timezone data need to be included in the code bundle. The development of DayJS corresponds to modern programming standards:  for instance, the latest changes in the DayJS repository date back to the current month, which indicates that its development and support are ongoing. In addition, anyone who has previously used MomentJS will have no issues with switching to DayJS which is another advantage.

Summing up

Despite its efficiency and rich functionality, Moment.js is becoming an outdated library with a lack of support from the development team. So as much as you might enjoy using it, now it’s probably the best time to consider migrating to Day.js and benefiting from its speed, quality of performance, and robust security. In addition, Day.js is rapidly expanding its functionality so we may assume that in some time, it will surpass Moment.js and will become the number one library for working with date and time.

Want to stay updated on the latest tech news?

Sign up for our monthly blog newsletter in the form below.

Softteco Logo Footer