In this article, we will learn how to get a total number of days in a month using javascript or jquery. There is no built-in function to get the number of days in a month in javascript. But we can achieve this by using this method as given below:
function daysInMonth (month, year) { // Use 1 for January, 2 for February, etc.
return new Date(year, month, 0).getDate();
}
console.log("Number of Days in Month: "+daysInMonth(2, 2019));
//OutPut ==> Number of Days in Month: 28
console.log("Number of Days in Month: "+daysInMonth(2, 2020));
//OutPut ==> Number of Days in Month: 29
console.log("Number of Days in Month: "+daysInMonth(6, 2020));
//OutPut ==> Number of Days in Month: 30
console.log("Number of Days in Month: "+daysInMonth(12, 2020));
//OutPut ==> Number of Days in Month: 31
Another way to get the number of days in a month by using third party libraries like Date.js and Moment.js.
I hope this article will help you to understand how to get a total number of days in a month using javascript or jquery. Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments