js获取当月最大天数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS 获取当月天数</title>
<script  type="text/javascript" language="javascript">
function getDays(){
//构造当前日期对象
var date = new Date();
 
//获取年份
var year = date.getFullYear();
 
//获取当前月份
var mouth = date.getMonth() + 1;
 
//定义当月的天数;
var days ;
 
//当月份为二月时,根据闰年还是非闰年判断天数
if(mouth == 2){
        days= year % 4 == 0 ? 29 : 28;
         
    }
    else if(mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12){
        //月份为:1,3,5,7,8,10,12 时,为大月.则天数为31;
        days= 31;
    }
    else{
        //其他月份,天数为:30.
        days= 30;
         
    }
     
    //输出天数
    alert('当月天数为:'+days);
}
 
</script>
</head>
<body>
<input type="button" value="查看当月最大天数" onclick="getDays()">
 
</body>
</html>
posted @ 2021-02-08 14:42  学无止境pk  阅读(449)  评论(0)    收藏  举报