Open In App

Moment.js moment().weekday(Number) Method

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

The moment().weekday() method is used to get or set the weekday of the Moment object. It is locale aware hence the value can depend based on whether the first day of the week is a Sunday or Monday. When the range of the weekday is exceeded on either side, it will set the weekday for the next or previous week.

Syntax:

moment().weekday( Number );

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

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

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

Example 1:

Javascript




const moment = require('moment');
  
console.log(
    "Current Date:", moment().toString()
)
console.log(
    "Current weekday is:", moment().weekday()
)
  
let thisWeekTuesday = moment().weekday(2);
console.log(
    "This week's Tuesday is:",
    thisWeekTuesday.toString()
)
  
let thisWeekFriday = moment().weekday(5);
console.log(
    "This week's Friday is:",
    thisWeekFriday.toString()
)
  
let thisWeekSunday = moment().weekday(7);
console.log(
    "This week's Sunday is:",
    thisWeekSunday.toString()
)


Output:

Current Date: Mon Jul 11 2022 01:30:15 GMT+0530
Current weekday is: 1
This week's Tuesday is: Tue Jul 12 2022 01:30:15 GMT+0530
This week's Friday is: Fri Jul 15 2022 01:30:15 GMT+0530
This week's Sunday is: Sun Jul 17 2022 01:30:15 GMT+0530

Example 2:

Javascript




const moment = require('moment');
  
console.log(
    "Current Date:", moment().toString()
)
console.log(
    "Current weekday is:", moment().weekday()
)
  
// Next week is 7 (full week) + 2 (for tuesday) = 9
let nextWeekTuesday = moment().weekday(9);
console.log(
    "Next week's Tuesday is:",
    nextWeekTuesday.toString()
)
  
// Previous week is 2 (for tuesday) - 7 (full week) = -5
let prevWeekTuesday = moment().weekday(-5);
console.log(
    "Previous week's Tuesday is:",
    prevWeekTuesday.toString()
)
  
// Next week is 7 (full week) + 7 (for sunday) = 14
let nextWeekSunday = moment().weekday(14);
console.log(
    "Next week's Sunday is:",
    nextWeekSunday.toString()
)


Output:

Current Date: Mon Jul 11 2022 01:30:15 GMT+0530
Current weekday is: 1
Next week's Tuesday is: Tue Jul 19 2022 01:30:15 GMT+0530
Previous week's Tuesday is: Tue Jul 05 2022 01:30:15 GMT+0530
Next week's Sunday is: Sun Jul 24 2022 01:30:15 GMT+0530

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads