某日是这一年第几天,用JavaScript的switch来写
分析
某日是这一年第几天
1 3 5 7 8 10 12 ------31天
4 6 9 11 -------30天
2 闰年29天 非闰年28天
var month = parseInt(prompt("请输入月"));
var day = parseInt(prompt("请输入日"));
var total = 0;
switch(month) {
case 12: total += 30; // 加上11月总天数
case 11: total += 31;// 加上10月总天数
case 10: total += 30; // total = total + 30;
case 9: total += 31;
case 8: total += 31;
case 7: total += 30;
case 6: total += 31;
case 5: total += 30;
case 4: total += 31;
case 3: {
var y = new Date().getFullYear();
if(y%400 === 0 || (y%4===0&&y%100!=0)) {
total += 29;
} else {
total += 28;
}
}
case 2: total += 31;
case 1: total += day;
}
alert(total);