var dt=new Date() // date object using today's date
alert(dt.getMonth())
getMonth() function returns number of month from 0 to 11. That is, it will return 0 if the month is January and will return 1 if the month is February and so on. It will return 11 for the month December.
date_object.getMonth()
To display the month name in full, we will write one array with all the month names inside it. Then based on the value returned by getMonth function we will display the corresponding element from this month array.
We will connect our button to display the current month in an alert window. Here is the demo.
Here is the code.
<html>
<head>
<title>Demo of getMonth in JavaScript</title>
<script type="text/javascript">
function show_now(){
var my_month=new Date()
var month_name=new Array(12);
month_name[0]="January"
month_name[1]="February"
month_name[2]="March"
month_name[3]="April"
month_name[4]="May"
month_name[5]="June"
month_name[6]="July"
month_name[7]="August"
month_name[8]="September"
month_name[9]="October"
month_name[10]="November"
month_name[11]="December"
alert ("Current month = " + month_name[my_month.getMonth()]);
}
</script>
</head>
<body >
<input type=button value="Show Month" onclick="show_now();">
</body>
</html>