Last updated on November 25, 2022
Day.js is a lightweight JavaScript library that deals with dates in an easy way. It has functions to parse, manipulate and validate both date and time. Day.js uses the same API that moment.js uses.
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 is an ultra-lightweight and fast JavaScript library, for displaying dates and times. Day.js library validates, manipulates, and parses dates and time-related information using moment js compatible API for modern browsers.
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.
The tree-shaking is a term used for JavaScript context to explain the removal of dead code. Dayjs supports the tree-shaking method with plugins.
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.
Dayjs works with vanilla JavaScript as well as node and Deno syntax. It means day js have great support with native vanilla JavaScript.
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>
In this article, we learned the advantages of day.js and two different ways of installation. If you would like to learn more, check out our JavaScript Page.