The getMonth()
method returns the month of a date as a number (0-11):
Example
const d = new Date();
document.getElementById("demo").innerHTML = d.getMonth();
document.getElementById("demo").innerHTML = d.getMonth();
In JavaScript, the first month (January) is month number 0, so December returns month number 11.
You can use an array of names, and getMonth()
to return the month as a name:
Example
const d = new Date();
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
document.getElementById("demo").innerHTML = months[d.getMonth()];
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
document.getElementById("demo").innerHTML = months[d.getMonth()];
Practice Excercise Practice now