if 写法 | 自己写法
let year=2022;
let month = 7;
let day=11;
function isRun(year) {
if((year%4==0 && year%100!=0) || year%400==0) {
return true;
}
return false;
}
function monthDays(m) {
if(m == 1 || m==3 || m==5 || m==7 || m ==8 || m==10 || m==12) {
return 31 + monthDays(m-1);
}
if(m ==2) {
return isRun()? 29 + monthDays(m-1) : 28 + monthDays(m-1);
}
if(m==4 || m==6 || m==9 || m==11){
return 30 + monthDays(m-1);
}
return 0;
}
console.log(day + monthDays(month-1));
switch 写法
let count=0;
function isRun(year){
if (year%400==0||(year%4==0&&year%100!==0)) {
return true;
}else{
return false;
}
}
function Gonedays(year,month,day){
switch (month) {
case 12 : count += 30;
case 11 : count += 31;
case 10 : count += 30;
case 9 : count += 31;
case 8 : count += 31;
case 7 : count += 30;
case 6 : count += 31;
case 5 : count += 30;
case 4 : count += 31;
case 3 : count += 28;
if (isRun(year)){
count++;
};
case 2 : count += 31;
case 1 : count += day;
break;
}
console.log(year+"年"+month+"月"+day+"日"+"过了"+count+"天");
}
Gonedays(2000,3,11);