VOOZH about

URL: https://www.geeksforgeeks.org/node-js/moment-js-moment-day-method/

⇱ Moment.js moment().day() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Moment.js moment().day() Method

Last Updated : 19 Sep, 2024

The moment().day() method in Moment.js returns or sets the day of the week for a moment object, where Sunday is represented as 0 and Saturday as 6. It helps manipulate and format weekday data easily in JavaScript.

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.

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.

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
Comment

Explore