当前位置:编程学习 > JS >>

javascript中获取当月的最后一天代码

最简单的代码

 代码如下 复制代码

<script type="text/javascript">
<!--
document.write(new Date(2009, 4, 1)); //2009年5月的第1天(1月的值是0),2009年5月1日
document.write(new Date(2009, 5, 0)); //2009年6月的第0天,也就是2009年5月的最后一天
//-->
</script>


另一种写法

 代码如下 复制代码

<html xmlns="http://www.w3.org/1999/xhtml" >
<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(2007,5))" />
</body>
</html>

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,