Switch to DayJS from MomentJS to Reduce JavaScript Bundle Size

Jason Kam
3 min readDec 19, 2022
Switch to DayJS from MomentJS to Reduce JavaScript Bundle Size

Why is DayJS Better than MomentJS?

Day.js is a lightweight JavaScript library for working with dates and times, and it is often cited as an alternative to Moment.js due to its smaller size and faster performance. Here are a few reasons why Day.js is considered to be better than Moment.js:

Follow Me to Stay Up to Date for More Tips & Tricks! 🔔😜

  1. Smaller size: Day.js is significantly smaller in size compared to Moment.js, making it a good choice for applications that need to minimize the size of the bundle. Day.js is only 2KB in size, while Moment.js is around 18KB.
  2. Faster performance: In addition to being smaller in size, Day.js is also generally faster than Moment.js in terms of performance. This is because Day.js uses a different approach to parsing and manipulating dates, which is generally more efficient than the approach used by Moment.js.
  3. Improved syntax: Some developers find the syntax of Day.js to be more intuitive and easier to use compared to Moment.js. For example, Day.js uses method chaining for many of its functions, which can make the code more readable.
  4. Better browser support: Day.js has better support for older browsers compared to Moment.js. Day.js can be used in browsers as old as Internet Explorer 9, while Moment.js requires at least Internet Explorer 10.

It’s worth noting that both Moment.js and Day.js are popular libraries for working with dates and times, and which one is better depends on the specific needs and preferences of the developer. Day.js may be a good choice for applications that need to minimize the size of the bundle and prioritize performance, while Moment.js may be a better choice for applications that require a large number of features or have more complex date and time manipulation requirements.

How to replace MomentJS with DayJS?

Here are the steps you can follow to replace momentjs with dayjs in your project:

  1. Install dayjs as a dependency in your project:
npm install dayjs

2. Import dayjs in your code wherever you are currently using momentjs:

import dayjs from 'dayjs';

3. Replace any instances of moment() with dayjs():

// Before
const date = moment().format('YYYY-MM-DD');
// After
const date = dayjs().format('YYYY-MM-DD');

4. Replace any instances of moment.utc() with dayjs.utc():

// Before
const date = moment.utc().format('YYYY-MM-DD');
// After
const date = dayjs.utc().format('YYYY-MM-DD');

5. Replace any instances of moment.duration() with dayjs.duration():

// Before
const duration = moment.duration(10, 'days');
// After
const duration = dayjs.duration(10, 'days');

6. Replace any instances of moment.isMoment() with dayjs.isDayjs():

// Before
if (moment.isMoment(date)) {
// Do something
}
// After
if (dayjs.isDayjs(date)) {
// Do something
}

7. If you are using any advanced features of momentjs that are not available in dayjs, you may need to find an alternative way to implement them using dayjs. For example, dayjs does not have a moment.defineLocale() function, so you will need to find another way to define locales if you are using this feature.

By following these steps, you should be able to successfully replace momentjs with dayjs in your project. Dayjs is a lightweight alternative to momentjs that provides many of the same features, but with a smaller footprint.

Thinks

Writing has always been my passion and it gives me pleasure to help and inspire people. If you have any questions, feel free to reach out!

Thank you for reading this far, please give this article a round of applause if you can, or give me a follow, I will thank you from the bottom of my heart

--

--