If you plan to build a new web application, we recommend the
day.js library for handling tasks related to date and time. In this article, we will learn to use
day.js and the advantages of day.js.
DayJS Object is immutable in all APIs that change the day.js Object and returns a new instance. It helps to avoid bugs and long debugging activities. As opposed to Moment JS is a mutable instance. In dayjs initial value of the variable can be modified and it supports method chaining.
It is a lightweight and fast library as compared with moment.js. The size of dayjs library is 2kb which works the same as the moment.js API.
Let’s install day.js through NPM. So run the below command in the terminal
npm install dayjs
Once you install it you can import it in your app. If your project is based on react then you need to import it by following,
import * as dayjs from 'dayjs';
If your project is based on node.js then you need to import it by following,
const dayjs = require('dayjs');
You can also use CDN (content delivery network) in the HTML document for adding day.js into it. In this case copy and paste below code in your HTML page.
<script src=" https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.5/dayjs.min.js"></script>