|
| JAVASCRIPT getMonth() METHOD
The getMonth() method in javascript returns the month as an integer from 0 through 11. January has a value of 0 through to December which has a value of 11.
The general format is:
dateObject.getMonth();
If you want to return the month with getMonth() method, you will first have to create a new date object and then use the new date object with the getMonth() method.
Example 1: Using getMonth() for this month:
<script type="text/javascript">
var mm=new Date(); // creates a new date object
mm=mm.getMonth(); // returns the month as a number
doument.write(mm); // outputs the month
</script>
The above will output:
Example 2: Using getMonth() to get month of specified date:
<script type="text/javascript">
var mm=new Date("December 16, 2005"); // creates a new date object
mm=mm.getMonth();
doument.write(mm);
</script>
The above will output:
Example 3: Using getMonth() and a switch statement to get the name of the month:
<script type="text/javascript">
var mm=new Date();
mm=mm.getMonth();
switch(mm)
{
case (0):
var MON="January";
break;
case (1):
var MON="February";
break;
case (2):
var MON="March";
break;
case (3):
var MON="April";
break;
case (4):
var MON="May";
break;
case (5):
var MON="June";
break;
case (6):
var MON="July";
break;
case (7):
var MON="August";
break;
case (8):
var MON="September";
break;
case (9):
var MON="October";
break;
case (10):
var MON="November";
break;
case (11):
var MON="December";
break;
}
document.write(MON);
</script>
The above outputs:
See Also:
|
|