Open In App
Related Articles

Moment.js Introduction

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Moment.js is a JavaScript package that makes it simple to parse, validate, manipulate, and display date/time in JavaScript. Moment.js allows you to display dates in a human-readable format based on your location. Moment.js can be used in a browser using the script approach. It is also compatible with Node.js and can be installed via npm.

Features of Moment.js:

  • Parsing: You can parse the date into the desired format using parsing. Date parsing is possible in the string, object, and array formats. It allows you to clone a moment with the help of moment.clone().
  • Manipulation: We can manipulate the Date and Time on the moment object using inbuilt methods provided by Moment.js. 
  • Date Validation: We can perform date validation using isValid() method provided by Moment.js. It also has various parsing flags to validate dates.
  • Date Queries: It has various inbuilt methods to check if the date is greater than or less than the input provided.
  • Duration: It is one of the essential features that handles the time length for the given units.
  • Display: It provides us with different formats to display date in different ways like JSON, array, object, etc.

Installing Moment.js: We can install moment.js 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 file in script tag inside the head section. 

<script type = "text/JavaScript" 
        src = 
" ">https://MomentJS.com/downloads/moment.js">
</script>

Method 2: We can install it using npm. Make sure that you have Node.js and npm installed.

npm install moment

Now let’s understand the working of Moment.js using an example.

Example: In this example, we will get the current time in minutes using Moment.js moment.minute() Method.

index.js

Javascript

// Importing moment module
const moment = require('moment');
 
let a = moment().minutes();
 
console.log("minute is: ",a);

                    

Step to run the application: 

Run the index.js file using the below command:

node index.js

Output:

minute is:  24

Last Updated : 20 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads