您的位置:首页 > Web前端 > JavaScript

JS获取一个月的最后一天

2015-08-05 19:20 639 查看
<html xmlns="http://www.w3.org/1999/xhtml" >
<meta charset="UTF-8">
<head>
<title>标题页</title>
<script language="javascript">
function getLastDay(year,month)
{
var new_year = year;  //取当前的年份
var new_month = month++;//取下一个月的第一天,方便计算(最后一天不固定)
if(month>12)      //如果当前大于12月,则年份转到下一年
{
new_month -=12;    //月份减
new_year++;      //年份增
}
var new_date = new Date(new_year,new_month,1);        //取当年当月中的第一天
return (new Date(new_date.getTime()-1000*60*60*24)).getDate();//获取当月最后一天日期
}
</script>
<body>
<input id="Button1" type="button" value="取2007年5月的最后一天" onClick="alert(getLastDay(2001,2))" />
</body>
</html>

当时间为2000,2月时显示28,当时间为2000时,显示29。

又测试了其他功能,测试通过,可用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript