Open In App

DayJS Introduction and Installation

Day js is a lightweight date and time manipulation library. It is an alternative to moment js with the same modern API. The latest version of day js has a size of 7Kb (minified) and 2Kb (Gzipped). Due to its small size, it takes less time to load and thus improving the overall performance of the application.

Features:



  1. It is easy to use and since it has the same API as moment js it is very much easy to work with it.
  2. It has gained a lot of popularity because of its small size.

Installation of DayJS:

const dayjs = require('dayjs');

Project Structure: The project structure will look like below image.

Filename- index.js:




const dayjs = require('dayjs');
  
// 2021-02-08T03:08:30+05:30 - ISO 8601 standard
console.log(dayjs().format());
  
// 08 February 2021, 03:08:30 AM
console.log(dayjs().format('DD MMMM YYYY, hh:mm:ss A')); 
  
// 08-02-2021
console.log(dayjs().format('DD-MM-YYYY'));
  
// Monday
console.log(dayjs().format('dddd')); 
  
// Feb
console.log(dayjs().format('MMM'));
    
// Feb 8th,21 
console.log(dayjs().format("MMM D[th],YY")); 
   

Run the file: In the terminal run the following command to execute the index.js file.

node index.js

Output:

The MomentJS Module does the same work as DayJS but when you have the performance of web application as the utmost priority, moment js can pose a problem because of its complex API and large bundle size. Dayjs is a great alternative to moment js with a very small size as compared to the moment js. So, this is how we can use DayJS in our application to manipulate, validate and display date and time.

Article Tags :