JavaScript | date.getUTCDay() Function
The date.getUTCDay() is an inbuilt function in JavaScript which is used to fetch the day of a week according to universal time from a given Date object.
Syntax:
DateObj.getUTCDay();
In the above syntax, DateObj is a valid Date object created using Date() conctructor from which we want to fetch day of a week accoding to universal time.
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 object according to universal time. 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 getUTCDay() method:
- Example 1:
<script>// Here a date has been assigned according to universal// time while creating Date objectvardateobj =newDate('October 15, 1996 05:35:32 GMT-11:00');// day of the week from above date object is// being extracted using getUTCDay()varB = dateobj.getUTCDay();// Printing day of the week// according to universal time.document.write(B);</script>chevron_rightfilter_noneOutput:
2
Errors and Exceptions:
- Example 1: The date of the month should lie in between 1 to 31 because none of the months have a date greater than 31. That is why the getUTCDay() function returns NaN when the date is greater than 31 i.e, not a number because the date for the month does not exist. So, Day of the week will also not exists when the date does not exist.
// Here a date has been assigned according to// universal time while creating a Date objectvardateobj =newDate('October 33, 1996 05:35:32 GMT-11:00');// day of the week from above date object is// being extracted using getUTCDay()varB = dateobj.getUTCDay();// Printing day of the week// according to universal time.document.write(B);</script>chevron_rightfilter_noneOutput:
NaN
- Example 2: If date of the month is not given, by default the getUTCDay() function consider it as first date of the month and hence accordingly it returns day of the week according to universal time. It is an exception case.
</script>// Here a date has been assigned according to universal// time while creating Date objectvardateobj =newDate('October 1996 05:35:32 GMT+11:00');// day of the week from above date object is// being extracted using getUTCDay().varB = dateobj.getUTCDay();// Printing day of the week// according to universal time.document.write(B);</script>chevron_rightfilter_noneOutput:
1
- Example 3: If nothing as parameter is given to the Date() constructor, the getUTCDay() function returns current day of the week.
<script>// Here nothing has been assigned according to universal// time while creating Date objectvardateobj =newDate();// day of the week from above date object is// being extracted using date.getUTCDay().varB = dateobj.getUTCDay();// Printing current day of the week// according to universal time.document.log(B);</script>chevron_rightfilter_noneOutput:
4
Here it returns 1 means Monday i.e, on first october 1996 there would be Monday.
Application: It has many applications such as getting a current day of the week according to universal time. Below program shows one of the applications of this function. It gives the current Day of the week according to universal time.
- Example 1:
<script>// Here nothing has been assigned according to universal// time while creating Date objectvardateobj =newDate();// day of the week from above date object is// being extracted using date.getUTCDay().varB = dateobj.getUTCDay();// Printing current day of the week// according to universal time.document.write(B);</script>chevron_rightfilter_noneOutput:
4
Supported Browsers: The browsers supported by JavaScript date.getUTCDay() Function are listed below:
- Google Chrome
- Interent Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- JavaScript | Function Parameters
- JavaScript | Function Definitions
- JavaScript | toString( ) function
- JavaScript | Math.pow( ) Function
- JavaScript | Array every() function
- JavaScript | String() Function
- JavaScript | Function Apply
- JavaScript | Function Call
- Javascript | Number() Function
- JavaScript | Math.E() function
- JavaScript | Array some() function
- JavaScript | toFixed( ) Function
- JavaScript | Array.of() function
- JavaScript | Math.abs( ) function
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.



