Moment.js moment.duration().days() Method Last Updated : 23 Jun, 2022 Comments Improve Suggest changes Like Article Like 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/ Comment More infoAdvertise with us Next Article Moment.js moment.duration(x.diff(y)) Method S sayantanm19 Follow Improve Article Tags : Web Technologies Node.js Moment.js Moment.js-Durations Similar Reads Moment.js moment.duration().add() Method The moment().duration().add() method is used to add a given time to the duration. The time to be added can be another Duration object, value in milliseconds, or a representation of the time using a string and numerical value. All the shorthands and keys used for creating durations can be used here f 4 min read Moment.js moment.duration().as(String) Method 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: T 2 min read Moment.js moment.duration().clone() Method The moment().duration().clone() Method is used to clone the given Duration object. As the Duration object is mutable, it can be used to save a copy of the current Duration like a snapshot by using this method, including all its properties and attributes. Syntax: moment().duration().clone(); Paramete 1 min read Moment.js moment.duration(x.diff(y)) Method The moment().duration(x.diff(y)) method is used to create a duration using the difference between two Moment objects by using the diff() method. Syntax: moment().duration( x.diff(y) ); Parameters: This method accepts a single parameter that uses the diff() method to find the difference between two M 1 min read Moment.js moment.duration().weeks() Method The moment().duration().weeks() method is used to get the weeks of the duration. This number of weeks is calculated as a subset of the days, therefore having a value between 0 and 4. The length of days for calculating each week is 7 days. This method is different from the asWeeks() method which retu 2 min read Moment.js moment.duration().hours() Method The moment().duration().hours() method is used to get the hours of the duration. This number of hours is calculated as a subset of a day, therefore having a value between 0 and 23. Syntax: moment().duration().hours(); Parameters: This method does not accept any parameters. Return Value: This method 2 min read Like