Day.js Advantages and Installation Steps

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.

Advantages of Day.js

  • Day.js is a modern and lightweight library.
  • In Day.js objects are Immutable.
  • Day.js works with the Tree-shaking method using plugins.
  • It is a minimal library as compared to moment.js.
  • Day.js supports Vanilla JavaScript Native Typescript, Node.js, and Deno.
  • It has simple syntax.
  • It has millions of weekly downloads that proves a large community loving it.
  • Day.js can be included by CDN (Content Delivery Network)

Installation Steps of Day.js

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 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 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>

Conclusion

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.