Why Moment.js ?
You may find numerous simple ways to add, subtract, validate dates, retrieve the maximum and minimum dates, and so on in Moment.js. It’s an open-source project, so you can easily contribute to it, add functionality in the form of plugins. You can parse the date into the desired format using parsing. We can also perform date validation.
Installing Moment.js: We can install it in the following two ways.
Method 1: Go to the official documentation of https://momentjs.com/ and download the latest moment.js file available and then add the link inside the script tag. Without downloading the file, you can also use CDN link to run the code.
Method 2: We can install it using npm. Make sure that you have Node.js and npm installed.
npm install moment
Example: In this example, we will get the current time in hours.
// Importing moment module
const moment = require('moment');
var a = moment().hours();
console.log("Hours is: ",a);
Run index.js file using below command:
Hours is: 22
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above