![]() |
Moment.js is a popular and open-source JavaScript library for validating, manipulating, and formatting dates. One common task is to determine the day of the week for a given date. This can be particularly useful in applications that need to perform actions based on the day of the week. In this article,we will explore various approaches to get the day of the week number using Moment.js. Table of Content To use Moment.js, we must ensure that we have installed it. We can do so by using the following command. Installing Moment.js in Node.jsnpm i moment There are two main approaches to get the day of the week number in Moment.js:
1. Using the day() MethodIn this approach we are using day() method of Moment.js which returns the day of the week as a number, where Sunday is 0 and Saturday is 6. This method is based on the ISO weekday numbering system. Syntax:moment().day(); Example: This example uses day() method to get day of week number in moment.js
Output: The day of the week for 2024-07-26 is: 5 2. Using the weekday() MethodIn this approach we are using the weekday() method in Moment.js which is similar to the day() method but follows the locale-aware week setting. This means it can return different values based on the locale settings. By default, it considers Monday as 0 and Sunday as 6. Syntax:moment().weekday(); Example: This example uses weekday() method to get day of week number in moment.js
Output: The weekday number for 2024-07-26 is: 1 |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 23 |