Open In App

Moment.js isMoment() Function

Improve
Improve
Like Article
Like
Save
Share
Report

It is used to check whether a variable is a particular moment or not in Moment.js using the isMoment() function that checks if a variable is a moment object, use moment.isMoment(). 

Syntax:

moment.isMoment(obj);

Parameter: Object Returns: True or False 

Installation of moment module:

You can visit the link to the Install moment module. You can install this package by using this command.

npm install moment

After installing the moment module, you can check your moment version in the command prompt using the command.

npm version moment

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

Example 1: Filename: index.js 

javascript




// Requiring module
const moment = require('moment');
 
let bool2 = moment.isMoment(new Date()); // false
console.log(bool2);
 
let bool3 = moment.isMoment(moment()); // true
console.log(bool3);


Steps to run the program:

Make sure you have installed the moment module using the following command:

npm install moment

Run the index.js file using the below command:

node index.js

Output:

false
true

Example 2: Filename: index.js 

javascript




// Requiring module
const moment = require('moment');
 
function checkMoment(data){
   return moment.isMoment(data);
}
 
let bool = checkMoment(moment());
console.log(bool);


Steps to run the application:

Run the index.js file using the below command:

node index.js

Output:

true

Reference: https://momentjs.com/docs/#/query/is-a-moment/


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