Open In App

Moment.js moment.duration().days() Method

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

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');
  
// This returns 28 as the number of
// days is less than a whole day
console.log(
    "durationOne days is:", durationOne.days()
)
  
// This returns 4 as the number of days
// is greater than a whole month
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');
  
// The asdays() method will return the
// length of the duration in days
console.log(
    "Length of durationA in days is:", durationA.asDays()
)
  
// This will return 4 as the number of complete days
console.log("durationA days is:", durationA.days())
  
console.log(
    "Length of durationB in days is:", durationB.asDays()
)
  
// This will return 3, as the 2 months 
// (or 61 days are considered) and 3 days
// remain of the new month
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
Previous
Next
Share your thoughts in the comments

Similar Reads