programming-examples/js/Date/Write a JavaScript function to get the number of days in a month..js

11 lines
426 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
var getDaysInMonth = function(month,year) {
// Here January is 1 based
//Day 0 is the last day in the previous month
return new Date(year, month, 0).getDate();
// Here January is 0 based
// return new Date(year, month+1, 0).getDate();
}
console.log(getDaysInMonth(1, 2012));
console.log(getDaysInMonth(2, 2012));
console.log(getDaysInMonth(9, 2012));
console.log(getDaysInMonth(12, 2012));