|
| JAVASCRIPT getDay() METHOD
The getDay() method in javascript returns the day of the week as an integer from 0 through 6. Sunday has a value of 0 through to Saturday which has a value of 6.
The general format is:
dateObject.getDay();
If you want to return the day of the week with getDay() method, you will first have to create a new date object and then use the new date object with the getDay() method.
Example 1: Using getDay() for today:
<script type="text/javascript">
var dd=new Date(); // creates a new date object
dd=dd.getDay(); // returns the day of the week
document.write(dd); // outputs the day of the week as a number
</script>
The above will output:
Example 2: Using getDay() to get day of week of a specified date:
<script type="text/javascript">
var dd=new Date("January 16, 2008"); // creates a new date object
dd=dd.getDay(); // returns the day of week as a number
document.write(dd); // outputs the month
</script>
The above will output:
Example 3: Using getDay() and a switch statement to get the name of the day:
<script type="text/javascript">
var dd=new Date();
dd=dd.getDay();
switch(dd)
{
case (0):
var DAY="Sunday";
break;
case (1):
var DAY="Monday";
break;
case (2):
var DAY="Tuesday";
break;
case (3):
var DAY="Wednesday";
break;
case (4):
var DAY="Thursday";
break;
case (5):
var DAY="Friday";
break;
case (6):
var DAY="Saturday";
break;
}
document.write(DAY);
</script>
The above outputs:
See Also:
|
|