在一个文件组合执行!
<script language=vbscript>
function MathDate(D)
If Not IsDate(D) then
MathDate = "用户输入不是日期!"
exit function
End if
D = CDate(D)
D = DateAdd("m",-1,D)
MathDate = FormatDateTime(D,2)
end function
</script><script language=javascript>
function callvb(){
outDate.value = MathDate(InDate.value);
}
</script><input type="text" name="InDate">
<input type="text" name="outDate">
<input type=button onclick="javascript:callvb();" value="计算">嘻嘻,完成功能,可以给分嘛?
<script language=vbscript>
function MathDate(D)
If Not IsDate(D) then
MathDate = "用户输入不是日期!"
exit function
End if
D = CDate(D)
D = DateAdd("m",-1,D)
MathDate = FormatDateTime(D,2)
end function
</script><script language=javascript>
function callvb(){
outDate.value = MathDate(InDate.value);
}
</script><input type="text" name="InDate">
<input type="text" name="outDate">
<input type=button onclick="javascript:callvb();" value="计算">嘻嘻,完成功能,可以给分嘛?
我需要的是完全的JavaScript的代码(某些浏览器不支持vbscript)!!!
可不可以提供一个纯JavaScript的代码???
function test(strDate){
switch (strDate){
case "2000-02-02":
alert("2000-01-02")
case "2000-03-31":
alert("2000-02-29")
case "1999-03-31":
alert("1999-02-28")
case "1999-01-31":
alert("1998-12-31")
default:
alert("no input")
}
}
</script><input name=a><br>
<input type=button onclick="test(a.value)" value="看结果">其实,我们不明白你的意思是什么???是要下月的当天日期吗?????
<script>
function test(strDate){
switch (strDate){
case "2000-02-02":
alert("2000-01-02");
break;
case "2000-03-31":
alert("2000-02-29");
break;
case "1999-03-31":
alert("1999-02-28");
break;
case "1999-01-31":
alert("1998-12-31");
break;
default:
alert("no input");
break;
}
}
</script><input name=a><br>
<input type=button onclick="test(a.value)" value="看结果">其实,我们不明白你的意思是什么???是要下月的当天日期吗?????
最好()支持各种日期格式的日期输入,
“YYYYMMDD”、
“YYYY-MM-DD”、
“YYYY-M-D”、
“YY-MM-DD”、
“YYYY/MM/DD”、
“YY/MM/DD”、
“YYMMDD”、
“MM-DD-YYYY”、
“MM/DD/YYYY”……
var msecondsPerMinute = 1000 * 60;
var msecondsPerHour = msecondsPerMinute * 60;
var msecondsPerDay = msecondsPerHour * 24;
var today = new Date();//今天的日期
today.setMonth(today.getMonth());
today.setDate(30);
var temptoday = new Date(today);//用来计算的这个月的1号
temptoday.setDate(1);
var before = new Date(today);//用来保存前一个月的日期
before.setMonth(today.getMonth()-1);
var tempbefore = new Date(before);//用来计算的前一个月的1号
tempbefore.setDate(1);
var next = new Date(today);//下个月,用来计算本月有多少天
next.setMonth(today.getMonth()+1);
next.setDate(1); var beforedaycount =Math.floor((temptoday-tempbefore)/msecondsPerDay);//计算前一个月天数
var todaydaycount =Math.floor((temptoday-tempbefore)/msecondsPerDay);//计算本月差额天数
if(todaydaycount>=beforedaycount){//如果本月天数大于前一个月
if(today.getDate()>=beforedaycount){//如果当前的天数大于上个月的天数,那么上个月返回末
before.setDate(beforedaycount);
}
else{//否则返回上个月相同号数的一天
before.setDate(today.getDate());
}
}
else{
if(today.getDate() == todaydaycount){//如果到了本月末尾,返回上月的最后一天
before.setDate(beforedaycount);
}
else{
before.setDate(today.getDate())//否则返回上个月相同号数的一天
}
}
alert(today);
alert(before);
return before;
}这回我测试了好几个日期,特殊的日期也测试过了。所以应该没有太大的问题了.有问题你就自己改好了,思路总是没有错的了。调的那么辛苦,别忘了给分哦!
function getLastMonthDate(objDate) {
var oldMonth = objDate.getMonth();
var oldDate = objDate.getDate();
var newMonth = (oldMonth>0)?(oldMonth-1):12;
objDate.setMonth(newMonth);
while (objDate.getMonth()==oldMonth) {
objDate.setMonth(newMonth);
objDate.setDate(oldDate--);
}
return objDate.toLocaleString();
}