2.输入一个日期,判定该天是该年的第几天,函数原型 int func (int year,int month,int day);例如,输入 2012 2 1, 输出32
<script type="text/javascript"> function days(year,month,day) { var days = 0; for(var i = 1; i < month; i++ ){ switch(i){ 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(isLeapYear(year)){ days += 29; } else { days += 28; } break; } default:{ alert("月份有误"); } } } days += day; return days; } //判断年份是 闰年还是平年 function isLeapYear(year){ if( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ return true; } else { return false; } } alert(days(2012,2,1)); </script>