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' ); // 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.
Please Login to comment...