Open In App

Moment.js moment.duration().isDuration(obj) Method

Last Updated : 28 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().isDuration() method is used to check if the given parameter is a Duration object. It returns a Boolean value. It will also return false if the object passed is a normal moment object.

Syntax:

moment().isDuration( obj );

Parameters: This method accepts a single parameter as mentioned above and described below:

  • obj: It is the object that will be checked if it is a Duration object.

Return Value: This method returns a Boolean value specifying if the parameter is a Duration object.

Note: This will not work in the normal Node.js program because it requires an external moment.js library to be installed globally or in the project directory.

Moment.js can be installed using the following command:

Installation of moment module:

npm install moment

The below examples will demonstrate the Moment.js moment().isDuration() Method.

Example 1:

Javascript




const moment = require('moment');
  
let durationOne = 
    moment.duration(10, 'minutes');
let durationTwo = 
    moment.duration({days: 4, minutes: 10});
let durationThree = moment();
  
console.log(
    "durationOne Is Duration:"
    moment.isDuration(durationOne)
)
console.log(
    "durationTwo Is Duration:",
    moment.isDuration(durationTwo)
)
console.log(
    "durationThree Is Duration:",
    moment.isDuration(durationThree)
)


Output:

durationOne Is Duration: true
durationTwo Is Duration: true
durationThree Is Duration: false

Example 2:

Javascript




const moment = require('moment');
  
let durationA = 
    moment.duration({hours: 4, minutes: 15});
let durationB = 
    durationA.clone();
let dateC = new Date()
  
console.log(
    "durationA Is Duration:",
    moment.isDuration(durationA)
)
console.log(
    "durationB Is Duration:",
    moment.isDuration(durationB)
)
console.log(
    "dateC Is Duration:",
    moment.isDuration(dateC)
)


Output:

durationA Is Duration: true
durationB Is Duration: true
dateC Is Duration: false

Reference: https://momentjs.com/docs/#/durations/is-a-duration/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads