2019年3月16日 星期六

農曆年與天干地支轉換

<html>
    <head>
        <title>農曆年與天干地支轉換程式</title>
        <script>
            /**
            *    民國13年為甲子年,並不是民國1年為甲子年
            */
            window.onload = function() {
                document.getElementById('sl').checked = false;
            }
    
            var sky = ["", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"];
            var land = ["", "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"];
            
            function sWord() {
                let sw = sky[document.getElementById('s').value];
                document.getElementById('sWord').innerText = typeof sw != 'undefined' ? sw : '';
            }
    
            function lWord() {
                let lw = land[document.getElementById('l').value];
                document.getElementById('lWord').innerText = typeof lw != 'undefined' ? lw : '';
            }
    
            /**
            *    勾選天干地支的顯示隱藏
            */
            function slCheck() {
                let sl = document.getElementById('sl').checked;
                let slView = document.getElementById('slView');
                let lunarSolarYear = document.getElementById('lunarSolarYear');
    
                if(sl) {
                    slView.style.display = 'block';
                    lunarSolarYear.style.display = 'none';
                    sWord();
                    lWord();
                } else {
                    slView.style.display = 'none';
                    lunarSolarYear.style.display = 'block';
                }
            }
    
            function display() {
                var num = document.getElementById('num').value;
                var sl = document.getElementById('sl').checked;
    
                if(!sl) {
                    // 農曆年轉天干地支
                    if(num <= 0) {
                        alert('請輸入正確的農曆年');
                        return;
                    }
                    if(num.length > 3) {
                        alert('輸入的農曆年不可超過千年');
                        return;
                    }
    
                    num = subtract60(num);
                    if(num.length == 1) num = '0' + num;
                    var skyResult = Number(num[1])-2 <= 0 ? sky[Number(num[1])+10-2]: sky[Number(num[1])-2];
                    alert(skyResult + land[getLandWord(Number(num))] + '年');
                } else {
                    // 天干地支轉農曆年
                    var s = document.getElementById('s').value;
                    var l = document.getElementById('l').value;
    
                    if(s > 10 || s <= 0) {
                        alert('天干必須在1~10之間');
                        return;
                    }
                    if(l > 12 || l <= 0) {
                        alert('地支必須在1~12之間');
                        return;
                    }
                    if((s % 2 == 1 && l % 2 == 0) || (s % 2 == 0 && l % 2 == 1)) {
                        alert('不正確的天干地支組合');
                        return;
                    }
    
                    var skyResult = Number(s) + 2;
                    if(skyResult >= 10) skyResult = skyResult - 10;
                    if(l.length == 1) l = '0' + l;
                    alert('農曆年為:' + getLandNumber(skyResult, l) + ' (或加減 60 的倍數)');
                }
            }
    
            /**
            *    取得地支數字
            */
            function getLandNumber(sky, land) {
                if(land.toString()[1] != sky) {
                    land = Number(land) + 12;
                    land = getLandNumber(sky, land)
                }
                return Number(land);
            }
    
            /**
            *    取得地支文字
            */
            function getLandWord(n){
                if(n>12) n = getLandWord(n-12);
                return n;
            }
    
            /**
            *    將農曆年縮小到60年之內
            */
            function subtract60(n){
                if(n>60) n = subtract60(n-60);
                return n.toString();
            }
        </script>
    </head>
    
    <body>
        <h1>農曆年與天干地支轉換程式</h1>
    
        是否使用天干地支轉農曆年<input type="checkbox" id="sl" onclick="slCheck()"><br />
        <span id='lunarSolarYear'>請輸入農曆年:
            <input type="number" id="num" style="width:70px" min='1' max='999' /><br />
        </span>
        <div id="slView" style='display:none'>
            請輸入天干:<input type="number" id="s" style="width:70px" min='1' max='10' onchange="sWord()" />
            <span id="sWord" style='color:blue'></span><br />
            請輸入地支:<input type="number" id="l" style="width:70px" min='1' max='12' onchange="lWord()" />
            <span id="lWord" style='color:blue'></span><br />
            1=甲, 2=乙, 3=丙, 4=丁, 5=戊, 6=己, 7=庚, 8=辛, 9=壬, 10=癸<br />
            1=子, 2=丑, 3=寅, 4=卯, 5=辰, 6=巳, 7=午, 8=未, 9=申, 10=酉, 11=戌, 12=亥<br />
        </div>
        <input type="button" value="確定" onclick="display()" />
    </body>
</html>

※農曆(陰陽合曆)13、73年為甲子年

※農曆年轉天干地支:個位數-2為天干,一直減到12之內為地支

※天干地支轉農曆年: 天干+2為個位數,地支較麻煩,以戊戌年來說
戊為5,加2為7,所以個位數為7
戌為11,11加多少的個位數會是7(天干的結果),必須加6才行,但還必須是12的倍數才行,60以下12的倍數有12、24、36、48,所以只有36,結果為11+36=47

沒有留言:

張貼留言