Adem's Dev Journey

Making the Switch from Moment.js to Day.js

05 Feb 2023 | 3 mins read

🔊 PLAY THIS ARTICLE

Moment.js is a popular JavaScript library for handling and formatting dates and times.

However, its size and complexity can often be a hindrance for web developers, especially when dealing with larger applications.

In this article, we will explore why Day.js, a relatively new library, is a better alternative to Moment.js and why you should consider making the switch.

Why Day.js

Day.js is designed to be a simpler and lighter alternative to Moment.js.

It has a smaller size, making it faster to download and easier to implement.

Additionally, Day.js is more intuitive to use, with a cleaner API and less confusing documentation, making it easier for developers to get started with it.

Day.js is also faster than Moment.js, which can be a crucial factor in large and complex applications. The performance improvements are due to the simpler architecture and smaller size of Day.js, which results in less overhead and fewer resources being used.

Day.js is built with modern JavaScript in mind, making it a better choice for developers who want to stay up-to-date with the latest advancements in the language. Additionally, Day.js is more easily maintainable than Moment.js, making it easier for developers to add new features and fix bugs as needed.

Installing Day.js

Installing Day.js is simple and straightforward. You can either download it from the official website or use a package manager like npm or yarn.

To install using npm, run the following command in your terminal:

npm install dayjs

Examples:

Parsing a date:

const date = dayjs('2022-01-01');
console.log(date.format('YYYY-MM-DD')); // 2022-01-01

Formatting a date:

const date = dayjs();
console.log(date.format('YYYY-MM-DD hh:mm:ss')); // 2023-02-06 08:25:08

Difference between two dates:

const date1 = dayjs('2022-01-01');
const date2 = dayjs('2022-12-31');
console.log(date2.diff(date1, 'day')); // 365

Adding or subtracting time:

const date = dayjs().add(1, 'month');
console.log(date.format('YYYY-MM-DD')); // 2023-03-06

const date = dayjs().subtract(1, 'year');
console.log(date.format('YYYY-MM-DD')); // 2022-02-06

These are just a few examples of what you can do with Day.js.

The library offers many more features and options, and you can find more information and examples in the official documentation.

https://www.npmjs.com/package/dayjs

Conclusion

In conclusion, Day.js is a simple, lightweight, and fast library for handling dates and times in JavaScript. Whether you are starting a new project or looking for a more efficient alternative to Moment.js, Day.js is definitely worth considering. Give it a try and see for yourself!