<html xmlns="http://www.w3.org/1999/xhtml" > <meta charset="UTF-8"> <head> <title>獲取當年當月最后一天日期</title> <script language="javascript"> function getLastDay(){ var year = document.getElementById('year').value; var month = document.getElementById('month').value; 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> year:<input id="year" value="" type="text"> <br /> mont:<input id="month" value="" type="text"> <br /> <input id="Button1" type="button" value="最后一天" onClick="alert(getLastDay())" /> </body> </html>