
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Weekday as a number in JavaScript?
In this article, we are going learn about the weekday as a number in JavaScript with appropriate examples.
To get the weekday as a number, JavaScript has provided getDay() method. The getDay() is a method from Date object. The getDay() method returns a value between 0 to 6. For example, Sunday = 0, Monday =1, Tuesday =2, Wednesday = 3 and so on.
To get a better understanding, let’s look into the syntax and usage of getDay() method in JavaScript.
Syntax
The syntax for getDay() method is −
dateObject.getDay();
Where, dateObject is an object created from date. This method returns a number between 0 to 6 (0 – Sunday, 1 – Monday, 2 – Tuesday, 3 – Wednesday, 4 – Thursday, 5 – Friday, 6 – Saturday).
Example 1
This is an example program to get the weekday of the current date as a number in JavaScript using getDay() method.
<!DOCTYPE html> <html> <head> <title>Weekday as a number in JavaScript</title> </head> <body style="text-align : center"> <h3>Weekday as a number in JavaScript</h3> <p>The returned value is an integer, which is the day of the week (0 to 6). i.e. 0 - Sunday 1 - Monday .... 6 - Saturday . </p> <p id="output"></p> <script> const date = new Date(); //Get the day of the week of current date const day = date.getDay(); document.getElementById("output").innerHTML = 'Week day as a number for the date : ' + date + ' is : ' + day; </script> </body> </html>
On executing the above code, the following output is generated.
Example 2
This is an example program to get the weekday of the user-mentioned date as a number in JavaScript using getDay() mehod.
<!DOCTYPE html> <html> <head> <title>Weekday as a number in JavaScript</title> </head> <body style="text-align : center"> <h3>Weekday as a number in JavaScript</h3> <p>The returned value is an integer, which is the day of the week (0 to 6). i.e. 0 - Sunday 1 - Monday .... 6 - Saturday . </p> <p id="output"></p> <script> const date = new Date("January 4 2001 04:04:04"); //Get the day of the week of particular date mentioned by the user const day = date.getDay(); document.getElementById("output").innerHTML = 'Week day as a number for the date : ' + date + ' is : ' + day; </script> </body> </html>
On executing the above code, the following output is generated.
Example 3
This is an example program to define a user defined function that is based on Zeller’s congruence to get weekday as a number in JavaScript.
<!DOCTYPE html> <html> <head> <title>Weekday as a number in JavaScript</title> </head> <body style="text-align : center"> <h3>Weekday as a number in JavaScript</h3> <p>The returned value is an integer, which is the day of the week (0 to 6). i.e. 0 - Sunday 1 - Monday .... 6 - Saturday . </p> <p id="output"></p> <script> // Without using Date() and getDay() var dd,mm,yyyy; function calculateDay(dd, mm, yyyy) { const weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; const indexes_of_weekday = [0,1,2,3,4,5,6]; if (mm< 3) { mm += 12; yyyy--; } var h = (dd + parseInt(((mm + 1) * 26) / 10) + yyyy + parseInt(yyyy / 4) + 6 * parseInt(yyyy / 100) + parseInt(yyyy / 400) - 1) % 7; return indexes_of_weekday[h]; } var day = calculateDay(23,06,2022); document.getElementById("output").innerHTML = 'The day is : ' + day; </script> </body> </html>
On executing the above code, the following output is generated.
- Related Articles
- How to Convert Weekday String to Number in Excel?
- Validate a number as Fibonacci series number in JavaScript
- Display weekday names in Java
- Decomposing rational number as a sum of rationals in JavaScript
- What is weekday() method in python?
- Calendar Functions in Python -(monthrange(), prcal(), weekday()?)
- How to print a number with commas as thousands of separators in JavaScript?
- Java Program to list Weekday names
- Representing number as the power and product of primes in JavaScript
- Python - Get the weekday from Timestamp object in Pandas
- Java Program to list Short Weekday Names
- Find Weekday using Zeller's Algorithm
- Ordering string as a number in a database?
- Reverse a number in JavaScript
- Factorize a number in JavaScript
