An introduction to Day.js with examples of useful functions

Dealing with dates and times in JavaScript is famously challenging. That’s why a library like Moment.js has almost 15 million weekly downloads on npm.

However, if you check the documentation of Moment.js, you will see that even they recommend some alternatives when it comes to modern development. One of the issues with Moment.js is the bundle size. If you need internationalization or time zone support, the bundle size can get quite large.

One alternative is the Day.js package, which is only 2kb.

Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for modern browsers with a largely Moment.js-compatible API.

If you used Moment.js before, using Day.js will feel very comfortable for you.

In this article, we will take a look at the Day.js package with some examples of the most common functions you will use.

Using Day.js

For our example, we will be working in a React application with JavaScript.

To get started with Day.js, install the package into your project.

npm install dayjs

Import dayjs and you can now use it in your file.

import dayjs from 'dayjs';

#time #date #javascript #programming

The Easiest Way to Deal With Dates and Times in JavaScript
1.25 GEEK