Open In App

Moment.js moment.duration().get(String) Method

Improve
Improve
Like Article
Like
Save
Share
Report

The moment().duration().get() Method is used to return the specified time value from a duration object. It is basically an alternative to other getter functions like weeks(), months(), hours() etc. It also supports all shorthand keys from the moment.add() method.

Syntax:

moment().duration().get(String);

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

  • String: This parameter is used to specify the unit of time that has to be returned.

Return Value: This method returns the requested unit of time from the duration. An invalid durations will return NaN for all values.

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.duration().get() Method.

Example 1:

Javascript




const moment = require('moment');
  
let durationOne = 
  moment.duration(2, 'days');
let durationTwo = 
  moment.duration(5, 'years');
let durationThree = 
  moment.duration(450, 'seconds');
  
// This is the same as .days() method
console.log(
  "durationOne get days is:",
  durationOne.get('days')
)
  
// This will return 0 as the number of months
// after a full year is considered is 0
// This is the same as .months() method
console.log(
  "durationTwo get months is:",
  durationTwo.get('months')
)
  
// This will return 7 as the number of minutes
// of the duration is 7
// This is the same as .minutes() method
console.log(
  "durationThree get minutes is:",
  durationThree.get('minutes')
)
  
// This will return 30 as the number of seconds
// after 7 full minutes is 30
// This is the same as .seconds() method
console.log(
  "durationThree get seconds is:",
  durationThree.get('seconds')
)


Output:

durationOne get days is: 2
durationTwo get months is: 0
durationThree get minutes is: 7       
durationThree get seconds is: 30

Example 2:

Javascript




const moment = require('moment');
  
let durationA =
  moment.duration({months: 4, days: 5, hours: 9});
let durationB = 
  moment.duration({hours: 73, minutes: 31});
  
console.log(
  "durationA get number of hours is:",
  durationA.get('hours')
)
console.log(
  "durationA get number of days is:",
  durationA.get('days')
)
console.log(
  "durationA get number of months is:",
  durationA.get('months')
)
  
console.log(
  "durationB get number of hours is:",
  durationB.get('hours')
)
console.log(
  "durationB get number of minutes is:",
  durationB.get('minutes'
))


Output:

durationA get number of hours is: 9
durationA get number of days is: 5
durationA get number of months is: 4
durationB get number of hours is: 1
durationB get number of minutes is: 31

Example 3: In this example, we will look at some the available shortcuts.

Javascript




const moment = require('moment');
  
let duration1 = 
  moment.duration({months: 4, days: 5, hours: 9});
let duration2 =
  moment.duration({hours: 73, minutes: 31});
  
console.log(
  "duration1 get number of hours is:",
  duration1.get('h')
)
console.log(
  "duration1 get number of days is:",
  duration1.get('d')
)
console.log(
  "duration1 get number of months is:",
  duration1.get('M')
)
  
console.log(
  "duration2 get number of hours is:",
  duration2.get('h')
)
console.log(
  "duration2 get number of minutes is:",
  duration2.get('m')
 )


Output:

duration1 get number of hours is: 9
duration1 get number of days is: 5
duration1 get number of months is: 4
duration2 get number of hours is: 1
duration2 get number of minutes is: 31

Reference: https://momentjs.com/docs/#/durations/get/



Last Updated : 25 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads