JavaScript判斷輸入的日期是今年的第幾天


const readline = require("readline-sync");
console.log("請輸入一個年份:");
let year = readline.question()-0;
while (isNaN(year) || year < 1 || year > 2018) {
    console.log("年份輸入有誤");
    year = readline.question()-0;
} 
console.log("請輸入一個月份:");
let month = readline.question()-0;
while (isNaN(month) || month < 1 || month > 12) {
    console.log("月份輸入有誤");
    month = readline.question()-0;
} 
console.log("請輸入一個日期:");
let day = readline.question()-0;
while (isNaN(day) || day < 1 || day >31) {
    console.log("日期輸入有誤");
    day = readline.question()-0;
} 
let alldays = 0;
switch (month - 1) {
    case 11:
        alldays += 30;
    case 10:
        alldays += 31;
    case 9:
        alldays += 30;
    case 8:
        alldays += 31;
    case 7:
        alldays += 31;
    case 6:
        alldays += 30;
    case 5:
        alldays += 31;
    case 4:
        alldays += 30;
    case 3:
        alldays += 31;
    case 2:
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            alldays += 29;
        } else {
            alldays += 28;
        }
    case 1:
        alldays += 31;
        break;
}
console.log(`${year}年${month}月${day}日是今年的第${alldays+day}天`);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM