Open In App

Moment.js moment().isoWeekday() Method

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

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

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

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

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

Example 1:

Javascript




const moment = require('moment');
  
console.log("Current Date:", moment().toString())
console.log("Current isoWeekday is:", moment().isoWeekday())
  
let thisWeekMonday = moment().isoWeekday(1);
console.log(
    "This week's Monday is:",
    thisWeekMonday.toString()
)
  
let thisWeekTuesday = moment().isoWeekday(2);
console.log(
    "This week's Tuesday is:",
    thisWeekTuesday.toString()
)
  
let thisWeekFriday = moment().isoWeekday("Friday");
console.log(
    "This week's Friday is:",
    thisWeekFriday.toString()
)
  
let thisWeekSunday = moment().isoWeekday("Sun");
console.log(
    "This week's Sunday is:",
    thisWeekSunday.toString()
)


Output:

Current Date: Mon Jul 18 2022 01:38:52 GMT+0530
Current isoWeekday is: 1
This week’s Monday is: Mon Jul 18 2022 01:38:52 GMT+0530
This week’s Tuesday is: Tue Jul 19 2022 01:38:52 GMT+0530
This week’s Friday is: Fri Jul 22 2022 01:38:52 GMT+0530
This week’s Sunday is: Sun Jul 24 2022 01:38:52 GMT+0530

Example 2:

Javascript




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


Output:

Current Date: Mon Jul 18 2022 01:38:52 GMT+0530
Current isoWeekday is: 1
Next week’s Tuesday is: Tue Jul 26 2022 01:38:52 GMT+0530
Previous week’s Tuesday is: Tue Jul 12 2022 01:38:52 GMT+0530
Next to next week’s Sunday is: Sun Aug 07 2022 01:38:52 GMT+0530

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads