Method
JavaScript Date getUTCDay() Method
The getUTCDay() method in JavaScript is used to get the day of the week in a universal time format. The getUTCDay() method returns a number that represents the day of the week, starting with 0 for Sunday and ending with 6 for Saturday. This method is very useful in scheduling tasks that are time dependent.
Syntax
The syntax for the getUTCDay() method is as follows:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
dateObject.getUTCDay()
[/dm_code_snippet]
Here, the dateObject is the Date object whose day of the week is to be returned.
Example
The following example shows the usage of the getUTCDay() method:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
var today = new Date();
var day = today.getUTCDay();
switch (day) {
case 0:
console.log("It's Sunday!");
break;
case 1:
console.log("It's Monday!");
break;
case 2:
console.log("It's Tuesday!");
break;
case 3:
console.log("It's Wednesday!");
break;
case 4:
console.log("It's Thursday!");
break;
case 5:
console.log("It's Friday!");
break;
case 6:
console.log("It's Saturday!");
break;
}
[/dm_code_snippet]
In the above example, the today variable is used to store the current date and the day variable is used to store the value returned by the getUTCDay() method. Then, a switch statement is used to display the name of the day of the week.
Output
When the above code is executed, it will produce the following output:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
It's Monday!
[/dm_code_snippet]