Open In App

Moment.js moment().isoWeeksInYear() Method

Last Updated : 14 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().isoWeeksInYear() method is used to return the number of ISO weeks in the current year of the Moment object. An ISO week-numbering system considers leap weeks in its system. This allows it to have only 52 or 53 full weeks. This is made possible by considering the number of days to be either 364 or 371 days instead of 365 or 366 days.

Syntax:

moment().isoWeeksInYear();

Parameters: This method does not accept any parameters.

Return Value: This method returns the number of ISO weeks in the current year of the Moment object.

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().isoWeeksInYear() Method.

Example 1:

Javascript




const moment = require('moment');
  
console.log("Current Date:", moment().toString())
console.log("Current year is:", moment().year())
console.log(
    "ISO Weeks in current year is:",
    moment().isoWeeksInYear()
)
  
let year2040 = moment().year(2040);
console.log(
    "ISO Weeks in Year 2040 is:",
    year2040.isoWeeksInYear()
)
  
let year1975 = moment().year(1975);
console.log(
    "ISO Weeks in Year 1975 is:",
    year1975.isoWeeksInYear()
)


Output:

Current Date: Wed Jul 13 2022 00:46:56 GMT+0530
Current year is: 2022
ISO Weeks in current year is: 52
ISO Weeks in Year 2040 is: 52
ISO Weeks in Year 1975 is: 52

Example 2:

Javascript




const moment = require('moment');
  
for (let i = 2020; i < 2040; i++) {
    console.log(
        `ISO Weeks in Year ${i} is: ${moment().year(i).isoWeeksInYear()}`
    )
}


Output:

ISO Weeks in Year 2020 is: 53
ISO Weeks in Year 2021 is: 52
ISO Weeks in Year 2022 is: 52
ISO Weeks in Year 2023 is: 52
ISO Weeks in Year 2024 is: 52
ISO Weeks in Year 2025 is: 52
ISO Weeks in Year 2026 is: 53
ISO Weeks in Year 2027 is: 52
ISO Weeks in Year 2028 is: 52
ISO Weeks in Year 2029 is: 52
ISO Weeks in Year 2030 is: 52
ISO Weeks in Year 2031 is: 52
ISO Weeks in Year 2032 is: 53
ISO Weeks in Year 2033 is: 52
ISO Weeks in Year 2034 is: 52
ISO Weeks in Year 2035 is: 52
ISO Weeks in Year 2036 is: 52
ISO Weeks in Year 2037 is: 53
ISO Weeks in Year 2038 is: 52
ISO Weeks in Year 2039 is: 52

Reference: https://momentjs.com/docs/#/get-set/iso-weeks-in-year/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads