Open In App

Moment.js moment().day() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The `moment().day()` function is utilized to retrieve or modify the day of the week within a Moment object. The day of the week is represented by a value ranging from 0 to 6, where 0 corresponds to Sunday and 6 corresponds to Saturday. If the specified value falls outside this range, it will wrap around to the previous or following weeks accordingly. It’s important to note that this method is not sensitive to locale settings, so the dates associated with the values will remain constant regardless of locale.

Syntax:

moment().weekday( Number );

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

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

Return Value: This method returns the current day of the 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

Example 1: The below examples will demonstrate the Moment.js moment().day() Method.

Javascript




const moment = require('moment');
 
console.log("Current Date:", moment().toString())
console.log("Current day is:", moment().day())
 
let thisWeekWednesday = moment().day(3);
console.log(
    "This week's Wednesday is:",
    thisWeekWednesday.toString()
)
 
let thisWeekSaturday = moment().day(6);
console.log(
    "This week's Saturday is:",
    thisWeekSaturday.toString()
)
 
let thisWeekMonday = moment().day(1);
console.log(
    "This week's Monday is:",
    thisWeekMonday.toString()
)


Output:

Current Date: Mon Jul 18 2022 01:21:54 GMT+0530
Current day is: 1
This week's Wednesday is: Wed Jul 20 2022 01:21:54 GMT+0530
This week's Saturday is: Sat Jul 23 2022 01:21:54 GMT+0530
This week's Monday is: Mon Jul 18 2022 01:21:54 GMT+0530

Example 2: The below examples will demonstrate the Moment.js moment().day() Method.

Javascript




const moment = require('moment');
 
console.log("Current Date:", moment().toString())
console.log("Current day is:", moment().day())
 
// Next week is 7 (full week) + 3 (for Wednesday) = 10
let nextWeekWednesday = moment().day(10);
console.log(
    "Next week's Wednesday is:",
    nextWeekWednesday.toString()
)
 
// Previous week is 3 (for Wednesday) - 7 (full week) = -4
let prevWeekWednesday = moment().day(-4);
console.log(
    "Previous week's Wednesday is:",
    prevWeekWednesday.toString()
)
 
// Next week is 7 (full week) + 7 (for sunday) = 14
let nextWeekSunday = moment().day(14);
console.log(
    "Next week's Sunday is:",
    nextWeekSunday.toString()
)


Output:

Current Date: Mon Jul 18 2022 01:21:54 GMT+0530
Current day is: 1
Next week's Wednesday is: Wed Jul 27 2022 01:21:54 GMT+0530
Previous week's Wednesday is: Wed Jul 13 2022 01:21:54 GMT+0530
Next week's Sunday is: Sun Jul 31 2022 01:21:54 GMT+0530


Last Updated : 15 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads