JS switch 输入一个月份 判断该月有多少天

时间:2025-02-10 08:37:15

1、接收用户数据

var year = parseInt(prompt('请您输入一个年份', 2000));
var month = parseInt(prompt('请您输入一个月份', 5));

2、判断每个月有多少天,记录每个月的天数

 var day = 0;
    switch (month) {
        case 1:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
        case 3:
            //(year, '年,', month, '月有', 31, '天');
                day = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
           // (year, '年,', month, '月有', 30, '天');
                day = 30;
            break;

3、特别处理2月

case 2:
      if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            day = 29;
             //(year, '年,', month, '有', 29, '天');
      } else {
            day = 28;
            //  (year, '年,', month, '有', 28, '天');
      }

4、输出结果

(year, '年,', month, '月有', day, '天');
('Game Over!!');