js判断一年中的多少天

字符串判断

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var year=2016;
        var moth=6;
        var day=22;
        var arr=[31,28,31,30,31,30,31,31,30,31,30,31];
        if(year%4===0 && year%100!==0 || year%400===0){
            arr[1]=29;
        }
      while(moth>1){
        day=arr[moth-2]+day;
        moth--;
      }
    console.log(day);
    </script>
</body>
</html>
 




switch判断
```<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var year=2016;
        var moth=1;
        var day=22;
        if(moth>3){
            if(year%4===0 && year%100!==0 || year%400===0){
                day+=1;
        }
        }
      while(moth>1){
            switch(moth-1){
                case  1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                     day=day+31;
                     break;
                case 2: 
                    day=day+28;
                    break;
                case 4:
                case 6:
                case 9:
                case 10:
                    day=day+30;
                    break;
            }
            moth--;
      }
      console.log(day);
    </script>
</body>
</html>
posted @ 2020-10-27 18:25  无敌臭弟弟  阅读(408)  评论(0编辑  收藏  举报