Open In App

Moment.js moment().isoWeekYear() Method

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

The moment().isoWeekYear() method is used to get or set the ISO week 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. This makes the method return the same date regardless of its locale.

Syntax:

moment().isoWeekYear( Number );

Parameters: This method accepts a single parameter as mentioned above and described below:

  • Number: It is the ISO Week Year that has to be set for the Moment object. it is an optional parameter.

Return Value: This method returns the current ISO week of the Moment.

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

Example 1:

Javascript




const moment = require('moment');
  
console.log("Current Date:", moment().toString())
console.log("Current isoWeekYear is:", moment().isoWeekYear())
  
let isoWeekYear1 = moment().isoWeekYear(1);
console.log(
    "Moment with isoWeekYear of 1 is:",
    isoWeekYear1.toString()
)
  
let isoWeekYear2 = moment().isoWeekYear(2);
console.log(
    "Moment with isoWeekYear of 2 is:",
    isoWeekYear2.toString()
)
  
let isoWeekYear2010 = moment().isoWeekYear(2010);
console.log(
    "Moment with isoWeekYear of 2010 is:",
    isoWeekYear2010.toString()
)
  
let isoWeekYear2024 = moment().isoWeekYear(2024);
console.log(
    "Moment with isoWeekYear of 2024 is:",
    isoWeekYear2024.toString()
)


Output:

Current Date: Mon Jul 18 2022 01:04:05 GMT+0530
Current isoWeekYear is: 2022
Moment with isoWeekYear of 1 is: Mon Jul 16 0001 01:04:05 GMT+0553
Moment with isoWeekYear of 2 is: Mon Jul 15 0002 01:04:05 GMT+0553
Moment with isoWeekYear of 2010 is: Mon Jul 19 2010 01:04:05 GMT+0530
Moment with isoWeekYear of 2024 is: Mon Jul 15 2024 01:04:05 GMT+0530

Example 2:

Javascript




const moment = require('moment');
  
console.log("Current Date:", moment().toString())
console.log(
    "Current isoWeekYear is:",
    moment().isoWeekYear()
)
  
let isoWeekYearDecember = moment().isoWeekYear(-100);
console.log(
    "Moment with isoWeekYear of -100 is:",
    isoWeekYearDecember.toString()
)
  
let isoWeekYearFeb = moment().isoWeekYear(-2020);
console.log(
    "Moment with isoWeekYear of -2020 is:",
    isoWeekYearFeb.toString()
)


Output:

Current Date: Mon Jul 18 2022 01:04:05 GMT+0530
Current isoWeekYear is: 2022
Moment with isoWeekYear of -100 is: Mon Jul 16 -0100 01:04:05 GMT+0553
Moment with isoWeekYear of -2020 is: Mon Jul 14 -2020 01:04:05 GMT+0553

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads