JavaScript | date.getDay() method
The date.getDay() is an inbuilt function in JavaScript which is used to fetch the day of a week from a given Date object.
Syntax:
DateObj.getDay()
In the above syntax, DateObj is a valid Date object created using Date() conctructor from which we want to fetch day.
Parameter: This function does not takes any parameter. It is just used along with a Date Object from which we want to fetch the day of the week.
Return Values: It returns the day of the week for the given date. Day of the week will be returned in the form of integer value ranging from 0 to 6 means 0 for Sunday, 1 for Monday and so on till 6 for Saturday.
Below program illustrate the getDay() method:
<script> // Here a date has been assigned // while creating Date object var A = new Date('October 15, 1996 05:35:32'); // day of the week from above Date Object is // being extracted using getDay() var B = A.getDay(); // Printing day of the week document.write(B); </script> |
Output:
2
Errors and Exceptions:
- Example 1: The date of the month should must lie in between 1 to 31 because none of the month have date greater than 31 that is why it returns NaN i.e, not a number because date for the month does not exist.
<script>// Here a date has been assigned// while creating Date objectvarA =newDate('October 35, 1996 05:35:32');// day of the week from above Date Object// is being extracted using getDay()varB = A.getDay();// Printing day of the week.document.write(B);</script>chevron_rightfilter_noneOutput:
NaN
- Example 2: If the date of the month is not given, by default the function consider it as the first date of the month and hence accordingly it returns day of the week. It is an exception case.
<script>// Here a date has been assigned// while creating Date objectvarA =newDate('October 1996 05:35:32');varB = A.getDay();// Printing daydocument.write(B);</script>chevron_rightfilter_noneOutput:
2
Here it returns 2 means Tuesday i.e. on first october 1996 there would be Tuesday.
- Example 3: If nothing as parameter is given, it returns current day of the current week.
<script>// Creating Date ObjectvarA =newDate();// day of the week from above object// is being extracted using getDay().varB = A.getDay()// Printing day of the week.document.write(B);</script>chevron_rightfilter_noneOutput:
5
Application: It has many applications such as getting the current day of the week. Below program shows one of the applications of this function. It gives the current Day.
<script> // Creating Date Object var A = new Date(); // day of the week from above object // is being extracted using getDay(). var B = A.getDay() // Printing day of the week. document.write(B); </script> |
Output:
5
Supported Browsers: The browsers supported by JavaScript date.getDay() Function are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- JavaScript | Array.from() method
- JavaScript | Array from() Method
- JavaScript | Array map() Method
- JavaScript | exec() Method
- JavaScript | Sort() method
- JavaScript | compile() Method
- JavaScript | getTime() Method
- JavaScript | Replace() Method
- JavaScript | String startsWith() Method
- JavaScript | removeEventListener() method with examples
- JavaScript | array.entries() Method
- Number valueOf( ) Method In JavaScript
- JavaScript | Array reduce() Method
- JavaScript | Window print() Method
- JavaScript | Array valueOf() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



