1 /*
2 * 名称:根据指定的月份获取该月有多少天
3 * 调用:getDaysCountByMonth(2013,5)
4 * 重载:getDaysCountByMonth('2013-05-22')
5 * getDaysCountByMonth('2013-05')
6 * getDaysCountByMonth('22/05/2013')
7 * getDaysCountByMonth('05/2013')
8 */
9 function getDaysCountByMonth(year, month) {
10 //方法重载判断
11 if(typeof(year)!='number' && month==undefined){
12 //提取字符串中的年、月
13 var temp=year;
14 var reg=/^\d{3,}|\d{3,}$/ig;
15 year=reg.exec(temp);
16 temp=temp.replace(/^\d{3,}\-|\/\d{3,}$/ig,'');
17 reg=/^\d+(?=\-)|\d+$/ig;
18 month=reg.exec(temp);
19 }
20 //创建一个新日期对象
21 var newDate = new Date(year,month);
22 //将日期设置为0,系统会自动转换为当月最大的日期
23 newDate.setDate(0);
24 //返回该月的最大日期(即该月的总天数)
25 return newDate.getDate();
26 }
27
28
29 //测试
30 alert(getDaysCountByMonth(2013,2)); //输出:28
31 alert(getDaysCountByMonth('2013-02-22')); //输出:28
32 alert(getDaysCountByMonth('2013-02')); //输出:28
33 alert(getDaysCountByMonth('02/2013')); //输出:28
34 alert(getDaysCountByMonth('22/02/2013')); //输出:28