JS--获取月份天数

代码实例:

/**
* 获取月份的天数
* 传入的month有可能是01、02、03...或者1、2、3....当month是01这种传入情况时需要去掉开头的0,以便从数组获取数据
* 传入的year是用来判断闰年和平年中二月份的天数
*/
function getDaysInMonth(month,year){
var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
if(month == '02' || month == 2){
year = parseInt(year);
daysInMonth[1] = (((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400)) ? 29 : 28;
}
var flag = month.substr(0,1);
if(flag == 0){
month = month.substr(1,1);
}
return daysInMonth[month-1];
}
原文地址:https://www.cnblogs.com/lfjblog/p/10175175.html