输入月份,判断这个月有几天

251 阅读1分钟

输入月份 判断天数
注意:二月份的话需要判断闰年还是平年 能被四整除且不能被100整除,或者能被400整除

        var month = prompt('请输入月份') * 1;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12: {
                console.log('31天');
                break;
            }
            case 4:
            case 6:
            case 8:
            case 10:
            case 12: {
                console.log('30天');
                break;
            }
            case 2: {
                var year = prompt('请输入年份') * 1;
                if ((!(year % 4) && (year % 100)) || !(year % 400)) {
                    console.log('29天');
                } else {
                    console.log("28天");
                }
                break;
            }
            default: {
                console.log('输入错误');
                break;
            }
        }