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 returns the length of the given duration in weeks.
Parameters: This method does not accept any parameters.
Return Value: This method returns the weeks (0-4) 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().weeks() Method.
durationOne Weeks is: 3 durationTwo Weeks is: 1
Example 2: This example will help to understand the difference of this method with asWeeks() for a better understanding.
Length of durationA in weeks is: 4.285714285714286 durationA Weeks is: 4 Length of durationB in weeks is: 5.428571428571429 durationB Weeks is: 1