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:
- It is easy to use and since it has the same API as moment js it is very much easy to work with it.
- It has gained a lot of popularity because of its small size.
Installation of DayJS:
- You can visit “https://day.js.org/docs/en/installation/installation” for the documentation. The package can be installed with this command.
npm install dayjs
- You can also add a CDN link to your project. The link is given below.
<script src=”https://unpkg.com/dayjs@1.8.21/dayjs.min.js”></script>
- After installing, you can check the installed version of the package with this command.
npm ls dayjs
- To start working with DayJS create a file with the name index.js. The package can be included in the index.js with this command.
const dayjs = require('dayjs');
Project Structure: The project structure will look like below image.
Filename- index.js:
Javascript
const dayjs = require( 'dayjs' );
console.log(dayjs().format());
console.log(dayjs().format( 'DD MMMM YYYY, hh:mm:ss A' ));
console.log(dayjs().format( 'DD-MM-YYYY' ));
console.log(dayjs().format( 'dddd' ));
console.log(dayjs().format( 'MMM' ));
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.
Last Updated :
08 Feb, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...