1. 程式人生 > 實用技巧 >JavaScript:輸入年月日,輸出當前年第多少天/多少周

JavaScript:輸入年月日,輸出當前年第多少天/多少周

 /*
       輸入年月日,輸出當前年第多少天/多少周?
 */
    var year  = 2018;
    var month = 8;
    var date  = 8;
    var sum   = 0; // 記錄總天數
    switch(month) {
        case 12:
            sum += 30;
        case 11:
            sum += 31;
        case 10:
            sum += 30;
        case 9:
            sum += 31;
        case 8:
            sum += 31;
        case 7:
            sum += 30;
        case 6:
            sum += 31;
        case 5:
            sum += 30;
        case 4:
            sum += 31;
        case 3:
            sum += 28;
            if (year % 2 == 0 && year % 100 != 0 || year % 400 == 0) {
                sum += 1;
            }
        case 2:
            sum += 31;
        case 1:
            sum += date;
            break;
        default : 
            alert("error");
            break;
    }
    
    var week = Math.ceil(sum / 7);
    alert("這是第" + sum + "天" + "這是" + week + "周");
    </script>