Open In App

Moment.js moment().week() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The moment().week() method is used to get or set the week of the Moment object. It is a locale aware method and hence the week of number will differ based on the locale of the Moment. This varies as the first day of the week can be either a Sunday or Monday according to the Locale.

Note: The day of the week would stay the same when this method is used for setting the week.

Syntax:

moment().week( Number );

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

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

Return Value: This method returns the current 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().week() Method.

Example 1:

Javascript




const moment = require('moment');
  
console.log("Current Date:", moment().toString())
console.log("Current week is:", moment().week())
  
let week1 = moment().week(1);
console.log(
    "Moment with Week of 1 is:",
    week1.toString()
)
  
let week40 = moment().week(40);
console.log(
    "Moment with Week of 40 is:",
    week40.toString()
)


Output:

Current Date: Wed Jul 13 2022 01:02:36 GMT+0530
Current week is: 29
Moment with Week of 1 is: Wed Dec 29 2021 01:02:36 GMT+0530
Moment with Week of 40 is: Wed Sep 28 2022 01:02:36 GMT+0530

Example 2: In this example, we will see how the different locales affect the specified week.

Javascript




const moment = require('moment');
  
console.log("Current Date:", moment().toString())
console.log("Current week is:", moment().week())
  
let week1en = moment().locale('en').week(1);
console.log(
    "Moment with Week of 1 with locale 'en' is:",
    week1en.toString()
)
  
let week1br = moment().locale('br').week(1);
console.log(
    "Moment with Week of 1 with locale 'br' is:",
    week1br.toString()
)
  
let week1in = moment().locale('in').week(52);
console.log(
    "Moment with Week of 52 with locale 'in' is:",
    week1in.toString()
)
  
let week1fr = moment().locale('fr').week(52);
console.log(
    "Moment with Week of 52 with locale 'fr' is:",
    week1fr.toString()
)


Output:

Current Date: Wed Jul 13 2022 01:02:36 GMT+0530
Current week is: 29
Moment with Week of 1 with locale ‘en’ is: Wed Dec 29 2021 01:02:36 GMT+0530
Moment with Week of 1 with locale ‘br’ is: Wed Jan 05 2022 01:02:36 GMT+0530
Moment with Week of 52 with locale ‘in’ is: Wed Dec 21 2022 01:02:36 GMT+0530
Moment with Week of 52 with locale ‘fr’ is: Wed Dec 28 2022 01:02:36 GMT+0530

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



Last Updated : 14 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads