请看如下代码!请高手修改一下!谢谢啊!怎么计算结果不对啊?
<html>
<head>
<script>
var yearsday=0,monthsday=0,zdays=0,year,day,month,m=0;
var year=parseInt(prompt("请输入年份"));
var month=parseInt(prompt("请输入月份"));
var day=parseInt(prompt("请输入天数"));
/*************************************************/
for(;year<2010;year++)
{
if(year%4==0&&year%100!=0||year%400==0)
{
day=366;
}
else
{
day=365;
}
yearsday=yearsday+day;
}
/*************************************************/
for(;m!=month;m++)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days=31; break;
case 4:
case 6:
case 9:
case 11:days=30; break;
/*************************************************/
case 2:
if(year%4==0&&year%100!=0||year%400==0)
{days=29;}
else{days=28;}
default:alert("请输入正确月份");break;
}
/*************************************************/
monthsday=monthsday+days;
}
zdays=yearsday+monthsday+day;
alert(zdays);
</script>
</head>
</html>
------解决方案--------------------
- HTML code
<html> <head> <script> var yearsday=0,monthsday=0,zdays=0,year,day,month,m=0; var year=parseInt(prompt("请输入年份")); var month=parseInt(prompt("请输入月份")); var day=parseInt(prompt("请输入天数")); var td = new Date(); var bd = new Date(year,month-1,day); var dif = td - bd; var days = Math.ceil(dif / (24 * 60 *60 * 1000)) alert(days) </script> </head> </html>
------解决方案--------------------
这个程序简直漏洞百出,
计算逻辑也不对,
用1楼的方法就挺好。
稍微改了改,能计算个大概:
- JScript code
var yearsday=0,monthsday=0,zdays=0,year,day,month,m=0; var year=parseInt(prompt("请输入年份")); var month=parseInt(prompt("请输入月份")); var date=parseInt(prompt("请输入天数")); /*************************************************/ for(;year<2010;year++) { if(year%4==0&&year%100!=0||year%400==0) { day=366; } else { day=365; } yearsday=yearsday+day; } /*************************************************/ for(var m=1;m<month;m++)//月份从1月份算起,算到输入的前一个月 { switch(m)//要判断m啊,判断month做什么??大哥 { case 1: case 3: case 5: case 7: case 8: case 10: case 12:days=31; break; case 4: case 6: case 9: case 11:days=30; break; /*************************************************/ case 2: if(year%4==0&&year%100!=0||year%400==0) {days=29;} else {days=28;} break;//加个这个 default:alert("请输入正确月份"); break; } /*************************************************/ monthsday = monthsday+days; } zdays=yearsday-(monthsday+date);//因为循环算年数的时候已经将所有天数的算上了,所以要减去出生那年你没有活的日子 alert(zdays);