Moment.js moment.duration().days() Method
Last Updated :
23 Jun, 2022
The moment().duration().days() method is used to get the days of the duration. This number of days is calculated as a subset of a month, therefore having a value between 0 and 30. The length of days for calculating each month is 31 days.
Syntax:
moment().duration().days();
Parameters: This method does not accept any parameters.
Return Value: This method returns the days (0-30) of the duration.
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().days() Method.
Example 1:
Javascript
const moment = require( 'moment' );
let durationOne = moment.duration(28, 'days' );
let durationTwo = moment.duration(35, 'days' );
console.log(
"durationOne days is:" , durationOne.days()
)
console.log(
"durationTwo days is:" , durationTwo.days()
)
|
Output:
durationOne days is: 28
durationTwo days is: 4
Example 2: This example will help to understand the difference of this method with asDays() for a better understanding.
Javascript
const moment = require( 'moment' );
let durationA = moment.duration(100, 'hours' );
let durationB = moment.duration(1550, 'hours' );
console.log(
"Length of durationA in days is:" , durationA.asDays()
)
console.log( "durationA days is:" , durationA.days())
console.log(
"Length of durationB in days is:" , durationB.asDays()
)
console.log( "durationB days is:" , durationB.days())
|
Output:
Length of durationA in days is: 4.166666666666667
durationA days is: 4
Length of durationB in days is: 64.58333333333333
durationB days is: 3
Reference: https://momentjs.com/docs/#/durations/days/
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...