获取某一年某一月有多少天

方法一:获取当前月份有多少天

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function getCurDays() {
        var curDate = new Date();
        curDate.setMonth(curDate.getMonth() + 1);
        curDate.setDate(0);
        alert(curDate.getDate());
}
</script>
</head>
<body onload="getCurDays()">
</body>
</html>

方法二:遍历某一年的每一个月有多少天

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function init(year){
var str = "";
for(var m = 1 ; m <= 12; m ++)
{
var dt = new Date();
dt.setFullYear(year);
dt.setMonth(m-1);
for(var d = 1; d<=32; d++)
{
dt.setDate(d)
if(dt.getDate() != d)
{
str += year+"年:"+m+"月有"+(d-1)+"天</br>"
break;
}
}
}
document.write(str)
}
</script>
</head>
<body onload="init(1990)">
</body>
</html>

方法三:判断某一年某一月有多少天

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function getCountDays(year,month) {
        var date = new Date();
	    date.setFullYear(year,month);      
       date.setDate(0);
       alert(date.getDate());
}
</script>
</head>
<body onload="getCountDays(1992,2)">
</body>
</html>



你可能感兴趣的:(html,Date,XHTML,文档)