📅  最后修改于: 2023-12-03 14:41:23.771000             🧑  作者: Mango
getMonth()
in JavaScriptJavaScript getMonth()
is a method available on the Date object. It returns the month in integer form from 0-11, where 0 represents January and 11 represents December.
dateObject.getMonth()
The dateObject
specifies the date you want to get the month from.
The getMonth()
method returns an integer between 0 and 11 representing the month of the year of the dateObject
.
const date = new Date();
const month = date.getMonth();
console.log(month); // returns current month e.g. if current month is April, it will return 3
getMonth()
with if
statementconst date = new Date();
const month = date.getMonth();
if(month === 0){
console.log('January');
}
else if(month === 1){
console.log('February');
}
else if(month === 2){
console.log('March');
}
//... continue for the remaining months
getMonth()
method returns an integer representing the month from 0 to 11, so you may need to add 1 to display the month in the correct format (e.g. January instead of 0).In conclusion, getMonth()
in JavaScript is a useful method for getting the month of the year from a given date object. With a simple syntax and a clear return value, it's easy to use in your code.