<HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE></TITLE> </HEAD> <BODY>年<INPUT type="text" id="year" name="year"> 月<INPUT type="text" id="month" name="month"> <INPUT type="button" value="Button" onclick="getLastDay()"></BODY> </HTML> <SCRIPT LANGUAGE=javascript> <!-- function getLastDay(){ var theDay = new Date(year.value,month.value-1); theDay = new Date(theDay.getFullYear(),theDay.getMonth()+1,theDay.getDate()-1); alert(theDay.getFullYear()+"-"+(theDay.getMonth()+1)+"-"+theDay.getDate()); } //--> </SCRIPT>
函数再优化一下:function getLastDay(){ var theDay = new Date(year.value,month.value,0); //theDay = new Date(theDay.getFullYear(),theDay.getMonth(),theDay.getDate()-1); alert(theDay.getFullYear()+"-"+(theDay.getMonth()+1)+"-"+theDay.getDate()); }
例如:var Today = '2003/05/01';
var Temp;
Temp = Today-1;
结果:Temp =‘NAN’
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>年<INPUT type="text" id="year" name="year">
月<INPUT type="text" id="month" name="month">
<INPUT type="button" value="Button" onclick="getLastDay()"></BODY>
</HTML>
<SCRIPT LANGUAGE=javascript>
<!--
function getLastDay(){
var theDay = new Date(year.value,month.value-1);
theDay = new Date(theDay.getFullYear(),theDay.getMonth()+1,theDay.getDate()-1);
alert(theDay.getFullYear()+"-"+(theDay.getMonth()+1)+"-"+theDay.getDate());
}
//-->
</SCRIPT>
var theDay = new Date(year.value,month.value,0);
//theDay = new Date(theDay.getFullYear(),theDay.getMonth(),theDay.getDate()-1);
alert(theDay.getFullYear()+"-"+(theDay.getMonth()+1)+"-"+theDay.getDate());
}
JavaScript中有 Date()对象.你要使用日期类型,new一个就是了.var date=new Date('2004-1-5');date=date-1;alert(date);
=>
theDay.getMonth()+2
得到的月份要比实际上小1
所以我用的是theDay.getMonth()+1
if(4,6,9,11) yyyy-mm-30
if(2) if(leap(year)) yyyy-02-29
else yyyy-02-28这样应该是非常省事儿了吧