Open In App

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

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

The moment().duration().as() Method returns the specified unit of time from the duration object. It is basically an alternative to other functions like asWeeks(), asMonths(), etc. It also supports all shorthand keys from the moment.add() method. 

Syntax:

moment().duration().as(String);

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

  • String: This parameter is used to specify the type of time value that the duration has to be converted.

Return Value: This method returns the requested value from the duration as the specified unit. An invalid duration 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().as() 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');
  
console.log(
  "durationOne as seconds is:",
  durationOne.as('seconds')
)
console.log(
  "durationTwo as days is:",
  durationTwo.as('days')
)
console.log(
  "durationThree as minutes is:",
  durationThree.as('minutes')
)


Output:

durationOne as seconds is: 172800
durationTwo as days is: 1826
durationThree as minutes is: 7.5

Example 2:

Javascript




const moment = require('moment');
  
let durationA =
  moment.duration({months: 4, days: 5, hours: 9});
let durationB =
  moment.duration({hours: 72, minutes: 30});
let durationC =
  moment.duration({years: 2, months: 6});
  
console.log(
  "durationA as number of hours is:",
  durationA.as('hours')
)
  
console.log(
  "durationB as number of days is:",
  durationB.as('days')
)
  
console.log(
  "durationC as number of years is:",
  durationC.as('years')
)


Output:

durationA as number of hours is: 3057
durationB as number of days is: 3.0208333333333335
durationC as number of years is: 2.5

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: 72, minutes: 30});
let duration3 = 
  moment.duration({years: 2, months: 6});
  
// Shorthand for Hours is 'h'
console.log(
  "duration1 as number of hours is:",
  duration1.as('h')
)
  
// Shorthand for Days is 'd'
console.log(
  "duration2 as number of days is:",
  duration2.as('d')
)
  
// Shorthand for Years is 'y'
console.log(
  "duration3 as number of years is:",
  duration3.as('y')
)


Output:

duration1 as number of hours is: 3057
duration2 as number of days is: 3.0208333333333335    
duration3 as number of years is: 2.5

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads