Open In App

Moment.js Customize Weekday

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Moment.js is very easy to customize. In general, you should create a locale setting with your customizations.

This article will discuss the moment.js Customize weekday in detail with examples. 

The moment().day() function allows us to add weekday to the locale customization. This will get/set on the day of the week. It takes input from 0-6, where 0 is for Sunday and six for Saturday. If the value exceeds the range, it will fall in the next week. You can set the day of the week using a number or string. It helps us to fulfill the need for more processing to calculate the weekdays. 

Syntax:

moment().day(Number|String);
moment().day();
moment().days(Number|String);
moment().days();

Parameters:

  • Number: The weekday that has to be set for the Moment object.

Returns: This method returns the current month 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 moment().weekday() function allows us to get or set the day of the week according to the locale. As per the locale, if Monday is set as the first day of the week, you will have to set the moment.weekday(0) set to “Monday”. If  “Tuesday” is the first day of the week, you will see moment.weekday(0) is set to “Tuesday”. The working of it remains the same as the day of the week where if greater than the range, it will be set to next week, if the value is -ve, it will go for last week.

Syntax:

moment().weekday(Number);
moment().weekday();

Example 1: In this example, we are simply using moment().day method getting customized weekdays in the short form of weekdays and getting at the end as the output.

Javascript




const moment = require('moment');
  
let a = moment().day();
console.log("The current weekday in number", a);
let b = moment().day(1);
console.log("The first day of the week:", b);
let c = moment().day('Monday');
console.log("Set the day of week to Sunday:", c);
let d = moment().day(15);
console.log("Number greater than 0-6 it sets to the next week to Monday:", d);
let e = moment().day(-10);
console.log("Putting he value -ve to set for last week:", e);


Output:

The current weekday in number 3
The first day of the week: Moment<2022-09-26T08:25:24+00:00>
Set the day of week to Monday: Moment<2022-09-26T08:25:24+00:00>
Number greater than 0-6 it sets to the next week to Monday: Moment<2022-10-10T08:25:24+00:00>
Putting he value -ve to set for last week: Moment<2022-09-15T08:25:24+00:00>

Example 2:  In this example, we are simply using moment().weekday() method getting customized weekdays in the short form of weekdays as per the local and getting the output in the end. 

Javascript




const moment = require('moment');
  
let a = moment().weekday();
console.log("The current weekday in number", a);
let b = moment().weekday(1);
console.log("The first day of the week:", b);
let c = moment().weekday('Monday');
console.log("Set the day of week to Sunday:", c);
let d = moment().weekday(15);
console.log("Number greater than 0-6 it sets to the next week to Monday:", d);
let e = moment().weekday(-10);
console.log("Putting the value -ve to set for last week", e);


Output:

The current weekday in number 3
The first day of the week: Moment<2022-09-26T08:27:05+00:00>
Set the day of week to Sunday: Moment<2022-09-28T08:27:05+00:00>
Number greater than 0-6 it sets to the next week to Monday: Moment < 2022-10-10T08:27:05+00:00>
Putting he value -ve to set for last week Moment < 2022-09-15T08:27:05+00:00>

The moment().isoWeekday() method will set /get the day of the week as per ISO where 1 is Monday and 7 is Sunday. So the range is 1-7 and anything greater than the range will fall in the next week and less than the range will fall in the last week.

Syntax:

moment().isoWeekday(Number);
moment().isoWeekday();

Example 3: In this example, we are simply using moment().isoWeekday method getting customized weekdays in the short form of weekdays as per the ISO and getting the output at the end.

Javascript




<script>      
      const moment = require('moment');
  
      var a = moment().isoWeekday(); 
      console.log("The current weekday in number",a);
      var b = moment().isoWeekday(1);
      console.log("The first day of the week:",b);
      var c = moment().isoWeekday('Monday');
      console.log("Set the day of week to Sunday:",c);
      var d = moment().isoWeekday(15); 
      console.log("Number greater than 0-6 it sets to the next week to Monday:",d)
      var e = moment().isoWeekday(-10);
      console.log("Putting he value -ve to set for last week",e)
</script>


Output:

The current weekday in number 3
The first day of the week: Moment<2022-09-26T08:28:33+00:00>
Set the day of week to Sunday: Moment<2022-09-26T08:28:33+00:00>
Number greater than 0-6 it sets to the next week to Monday: Moment<2022-10-10T08:28:33+00:00>
Putting he value -ve to set for last week Moment<2022-09-15T08:28:33+00:00>


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads