Day.js Alternative for MomentJS
Working with Dates in Javascript is a nightmare for those who don’t want to make their hand’s dirty with the Date object.So mostly we will use the libraries that is available to parse, validate, manipulate and format dates.
With that in mind most of the developers will use the most popular libraries that is available. In that the most popular one was Moment.js.
I have been using Moment.js for most of my projects where i am not aware of that Moment.js has become legacy project in maintenance mode.
Now it’s time to look at the alternative for Moment.js. So in this post we will see the following
Table of Contents
MomentJS Overview
Day.js Overview
Bundle Size
Npm Trends
The Future
1. MomentJS Overview
MomentJS is the most popular library for dealing with dates, but it has a lot of drawbacks compared to the newer libraries designed to work with dates.
Even the Moment Project team considers it a completed project and would not provide new features.
We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.
In practice, this means Moment:
Will not be adding new features or capabilities.
Will not be changing Moment’s API to be immutable.
Will not be addressing tree shaking or bundle size issues.
Will not be making any major changes (no version 3).
May choose to not fix bugs or behavioral quirks, especially if they are long-standing known issues.
It doesn’t means that you should not use Moment in your new project.However there are some possible reasons you might want to keep using it.
Browser support
Moment works well on Internet Explorer 8 and higher.Other libraries have also had issues with Safari, especially on mobile devices. If you have a strong requirement to support older browsers, then you might want to stick with Moment for a bit longer.
However, Day.js reports compatibility with IE8 and higher so you still may wish to consider that alternative.
Dependencies by Other libraries
Several other libraries, especially date pickers and graphing libraries, take Moment as a dependency. If you are using such a component and cannot find an alternative, then you are already including Moment in your project. Thus, it might make sense to continue using Moment throughout your project rather than including yet another date and time library.
2. Day.js Overview
Dayjs is a minimalist JavaScript library that parses, validates, manipulates, and displays date and time information for modern browsers using a largely MomentJS-compatible API.
If you are used to work with MomentJS and want to get out quickly, dayjs may be the best option to start with.
Immutable
All API operations that change the Day.js object will return a new instance instead.This helps prevent bugs and avoid long debugging sessions.
I18n
Day.js has great support for internationalization. But none of them will be included in your build unless you use them.
3. Bundle Size
Now we will see the bundle size of both MomentJS and Day.js which is used in the Angular application
MomentJS Bundle Size
If you see the above image you could see the size of MomentJS and MomentJS Locale as follows
Stat size: 562.28 KB
Parsed size: 371.24 KB
Gzipped size: 75.58 KB
Whereas if you look at the Day.js bundle size it is 96% smaller than MomentJS
Day.js bundle size
Stat size: 9.87 KB
Parsed size: 6.48 KB
Gzipped size: 2.91 KB
MomenJS Bundle size
DayJS Bundle size
4. Npm Trends
But if we look at the Npm trends we could see that MomentJS is still the popular library to handle dates
Npm Trends
As we mentioned earlier that this post is not about using Day.js It is to check out the alternatives for MomentJS and moreover it is your decision to go either with MomentJS or Day.js and moreover Day.js is not the only alternative there are few more alternatives such as
Luxon
Date-fns
Npm trends of all date libraries
5. The Future
Temporal — Better dates and times in the JavaScript language!
Soon, there won’t be a strong need for date and time libraries in JavaScript at all. Instead, we will be able to use capabilities of the JavaScript language itself. Though some capabilities are here today with [Date](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date)
and [Intl](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl).
The effort to make better date and time APIs in the JavaScript language is being done via The ECMA TC39 Temporal Proposal. It is currently at Stage 3 of the TC39 process.
Temporal
will be a new global object that acts as a top-level namespace (like Math
). It exposes many separate types of objects including Temporal.Instant
, Temporal.ZonedDateTime
, Temporal.PlainDateTime
, Temporal.PlainDate
, Temporal.PlainTime
, Temporal.TimeZone
and several others. The Temporal Cookbook shows many "recipes" with examples of how these objects can be used in different scenarios.
Resource Links
MomentJS — https://momentjs.com/
Day.js — https://day.js.org/en/
BundlePhobia — https://bundlephobia.com/package/moment@2.29.1
Congratulations!
Today you have seen an alternative for MomentJS and it doesn’t mean that you shouldn’t use MomentJS.Try to play with Day.js if you wish else play with MomentJS and explore the new ways.
Will Catch you up in a new post till then Happy Learning:)
Subscribe to my newsletter
Read articles from nidhinkumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by