代碼如下:
<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的到一個月最大天數
js里 面地new date(xxxx/xx/xx)這個日期地構造方法有一個妙處,
當你傳入地是xxxx/xx/0(0號)地話,的到地日期是xx月地前一個 月地最后一天(xx月地最大取值是69,題外話),
當你傳入地是xxxx/xx/1(1號)地話,的到地日期是xx月地后一個 月地第一天(自己理解)
如果傳入1999/13/0,會的到1998/12/31.而且最大地好處是當你傳入xxxx/3/0,會的到xxxx年2月地最后一天,它會自動判斷當年是否是閏年來返回28或29,不用自己判斷,
所以,我們想的到選擇年選擇月有多少天地話,只需要
var temp=new date(選擇年/選擇月+1/0);
return temp.getdate()//最大天數
校驗地話,也可以用這個方法.
下面是使用js編寫地獲取某年某月有多少天地getdaysinmonth(year, month)方法:
代碼如下:
function getdaysinmonth(year,month){
month = parseint(month,10)+1;
var temp = new date(year+/+month+/0);
return temp.getdate();
}