Open In App

Moment.js moment().isoWeek() Method

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

The moment().isoWeek() method is used to get or set the ISO week 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().isoWeek( Number );

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

  • Number: It is the ISO week 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().isoWeek() Method.

Example 1:

Javascript




const moment = require('moment');
  
console.log("Current Date:", moment().toString())
console.log("Current isoWeek is:", moment().isoWeek())
  
let isoWeek1 = moment().isoWeek(1);
console.log(
    "Moment with isoWeek of 1 is:",
    isoWeek1.toString()
)
  
let isoWeek40 = moment().isoWeek(40);
console.log(
    "Moment with isoWeek of 40 is:",
    isoWeek40.toString()
)


Output:

Current Date: Wed Jul 13 2022 01:12:39 GMT+0530
Current isoWeek is: 28
Moment with isoWeek of 1 is: Wed Jan 05 2022 01:12:39 GMT+0530
Moment with isoWeek of 40 is: Wed Oct 05 2022 01:12:39 GMT+0530

Example 2: In this example, we will see that the ISO week is not affected by the locale of the Moment, hence all the dates are the same for a week.

Javascript




const moment = require('moment');
  
let isoWeek1en = moment().locale('en').isoWeek(1);
console.log(
    "Moment with isoWeek of 1 with locale 'en' is:",
    isoWeek1en.toString()
)
  
let isoWeek1br = moment().locale('br').isoWeek(1);
console.log(
    "Moment with isoWeek of 1 with locale 'br' is:",
    isoWeek1br.toString()
)
  
let isoWeek1in = moment().locale('in').isoWeek(52);
console.log(
    "Moment with isoWeek of 52 with locale 'in' is:",
    isoWeek1in.toString()
)
  
let isoWeek1fr = moment().locale('fr').isoWeek(52);
console.log(
    "Moment with isoWeek of 52 with locale 'fr' is:",
    isoWeek1fr.toString()
)


Output:

Moment with isoWeek of 1 with locale ‘en’ is: Wed Jan 05 2022 01:12:39 GMT+0530
Moment with isoWeek of 1 with locale ‘br’ is: Wed Jan 05 2022 01:12:39 GMT+0530
Moment with isoWeek of 52 with locale ‘in’ is: Wed Dec 28 2022 01:12:39 GMT+0530
Moment with isoWeek of 52 with locale ‘fr’ is: Wed Dec 28 2022 01:12:39 GMT+0530

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads