JAVASCRIPT

 Home  Computers & Internet  Web Programming JAVASCRIPT
What is Javascript?
Javascript Placement
Syntax
Reserved Words
Variables
Data Types
Escaping Characters
Concatenation
Arithmetic Operators
Assignment Operators
Comparison Operators
Boolean Operators
Conditional Operator
If Statements
Else If Statements
If Else Statements
Switch Statements
While Loops
For Loops
Do While Loops
Break Statement
Continue Statement
prompt()
alert()
Date()
Event Handlers
String Object Methods
Math Object Methods
Window Object Methods
Arrays

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:

getDate() getDay() getMonth()
getFullYear() getYear() getHours()
getMinutes() getSeconds() getTime()
getTimezoneOffset() parse() setDate()
setFullYear() setHours() setMinutes()
setMonth() setSeconds() setTime()
setYear() toGMTString() toLocaleString()
toString() getUTCDate() getUTCDay()
getUTCMonth() getUTCFullYear() getUTCHours()
getUTCMinutes() getUTCSeconds() getUTCMilliseconds()
setUTCDate() setUTCDay() setUTCMonth()
setUTCFullYear() setUTCHour() setUTCMinutes()
setUTCSeconds() setUTCMilliseconds()  
Home | Privacy Policy | Terms Of Use | Contact Us