农历 公历 互转函数(包括节气获取)

由于工作上需要实现公历转换农历.以及农历转换成公历.还有农历公历二级联动.特封装了这个转换函数. 有个函数的对照表对于农历的联动也很容易实现了.

// JS日历转化公用类
// @date   2010-06-01

function tagLunarCal( d, i, w, k, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13) {
    this.BaseDays = d;         /* 1 月 1 日到正月初一的累计日 */
    this.Intercalation = i;    /* 闰月月份. 0==此年沒有闰月 */
    this.BaseWeekday = w;      /* 此年 1 月 1 日为星期减 1 */
    this.BaseKanChih = k;      /* 此年 1 月 1 日之干支序号减 1 */
    this.MonthDays = [ m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13 ]; /* 此农历年每月之大小, 0==小月(29日), 1==大月(30日) */
}

// 闰年判断.返回1 或 0
function GetLeap(year) {
    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 1 : 0;
}

// 主类入口
// @param {Date} new Date(y,m,d)
// @param {Boolean} 0: 农历-->公历 1: 公历 --> 农历
// @return {String} 见example
// @example
// CalConvert(new Date(2010,3,19), false) 即农历的四月十九转为公历  结果为 2010-06-01
// CalConvert(new Date(2010,5,1), true)   即公历的6月1日转为农历    结果为 2010年四月十九
//
function CalConvert(date, sign) {
    // 日期上下限
    FIRSTYEAR = 1936;
    LASTYEAR = 2031;
    // 返回结果值
    RESULT = 0;
    // 对照表
    LunarCal = [
        new tagLunarCal(23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ), /* 1936 */ 
        new tagLunarCal( 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ), 
        new tagLunarCal( 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ), 
        new tagLunarCal( 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ), 
        new tagLunarCal( 38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 1940 */ 
        new tagLunarCal( 26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ), 
        new tagLunarCal( 24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1944 */ 
        new tagLunarCal( 43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ), 
        new tagLunarCal( 32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ), 
        new tagLunarCal( 21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ), 
        new tagLunarCal( 40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ), /* 1948 */ 
        new tagLunarCal( 28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), 
        new tagLunarCal( 47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1 ), 
        new tagLunarCal( 36, 0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 26, 5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ), /* 1952 */ 
        new tagLunarCal( 44, 0, 3, 47, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1 ), 
        new tagLunarCal( 33, 0, 4, 52, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), 
        new tagLunarCal( 23, 3, 5, 57, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ), 
        new tagLunarCal( 42, 0, 6, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ), /* 1956 */ 
        new tagLunarCal( 30, 8, 1, 8, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 48, 0, 2, 13, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 38, 0, 3, 18, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), 
        new tagLunarCal( 27, 6, 4, 23, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 1960 */ 
        new tagLunarCal( 45, 0, 6, 29, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 35, 0, 0, 34, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), 
        new tagLunarCal( 24, 4, 1, 39, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ), 
        new tagLunarCal( 43, 0, 2, 44, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ), /* 1964 */ 
        new tagLunarCal( 32, 0, 4, 50, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ), 
        new tagLunarCal( 20, 3, 5, 55, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ), 
        new tagLunarCal( 39, 0, 6, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 29, 7, 0, 5, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 1968 */ 
        new tagLunarCal( 47, 0, 2, 11, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ), 
        new tagLunarCal( 36, 0, 3, 16, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ), 
        new tagLunarCal( 26, 5, 4, 21, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 ), 
        new tagLunarCal( 45, 0, 5, 26, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1972 */ 
        new tagLunarCal( 33, 0, 0, 32, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1 ), 
        new tagLunarCal( 22, 4, 1, 37, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ), 
        new tagLunarCal( 41, 0, 2, 42, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ), 
        new tagLunarCal( 30, 8, 3, 47, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ), /* 1976 */ 
        new tagLunarCal( 48, 0, 5, 53, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1 ), 
        new tagLunarCal( 37, 0, 6, 58, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ), 
        new tagLunarCal( 27, 6, 0, 3, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ), 
        new tagLunarCal( 46, 0, 1, 8, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 1980 */ 
        new tagLunarCal( 35, 0, 3, 14, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1 ), 
        new tagLunarCal( 24, 4, 4, 19, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ), 
        new tagLunarCal( 43, 0, 5, 24, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ), 
        new tagLunarCal( 32, 10, 6, 29, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ), /* 1984 */ 
        new tagLunarCal( 50, 0, 1, 35, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 39, 0, 2, 40, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1 ), 
        new tagLunarCal( 28, 6, 3, 45, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 ), 
        new tagLunarCal( 47, 0, 4, 50, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 1988 */ 
        new tagLunarCal( 36, 0, 6, 56, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ), 
        new tagLunarCal( 26, 5, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1 ), 
        new tagLunarCal( 45, 0, 1, 6, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0 ), 
        new tagLunarCal( 34, 0, 2, 11, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0 ), /* 1992 */ 
        new tagLunarCal( 22, 3, 4, 17, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 40, 0, 5, 22, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ), 
        new tagLunarCal( 30, 8, 6, 27, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1 ), 
        new tagLunarCal( 49, 0, 0, 32, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1 ), /* 1996 */ 
        new tagLunarCal( 37, 0, 2, 38, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), 
        new tagLunarCal( 27, 5, 3, 43, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1 ), 
        new tagLunarCal( 46,  0, 4, 48, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ), /* 1999 */
        new tagLunarCal( 35,  0, 5, 53, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ), /* 2000 */
        new tagLunarCal( 23,  4, 0, 59, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
        new tagLunarCal( 42,  0, 1,  4, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
        new tagLunarCal( 31,  0, 2,  9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
        new tagLunarCal( 21,  2, 3, 14, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 2004 */
        new tagLunarCal( 39,  0, 5, 20, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
        new tagLunarCal( 28,  7, 6, 25, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ),
        new tagLunarCal( 48,  0, 0, 30, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
        new tagLunarCal( 37,  0, 1, 35, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 2008 */
        new tagLunarCal( 25,  5, 3, 41, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
        new tagLunarCal( 44,  0, 4, 46, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
        new tagLunarCal( 33,  0, 5, 51, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
        new tagLunarCal( 22,  4, 6, 56, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 2012 */

        new tagLunarCal( 40,  0, 1,  2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
        new tagLunarCal( 30,  9, 2,  7, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
        new tagLunarCal( 49,  0, 3, 12, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 ),
        new tagLunarCal( 38,  0, 4, 17, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 2016 */
        new tagLunarCal( 27,  6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
        new tagLunarCal( 46,  0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 ),
        new tagLunarCal( 35,  0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
        new tagLunarCal( 24,  4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 2020 */
        new tagLunarCal( 42,  0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
        new tagLunarCal( 31,  0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ),
        new tagLunarCal( 21,  2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
        new tagLunarCal( 40,  0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 2024 */
        new tagLunarCal( 28,  6, 2,  5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
        new tagLunarCal( 47,  0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 ),
        new tagLunarCal( 36,  0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
        new tagLunarCal( 25,  5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ), /* 2028 */
        new tagLunarCal( 43,  0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ),
        new tagLunarCal( 32,  0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 ),
        new tagLunarCal( 22,  3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ) ];

        /* 西曆年每月之日數 */
        SolarCal = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
        /* 西曆年每月之累積日數, 平年與閏年 */
        SolarDays = [
            0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396,
            0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 ];

        AnimalIdx = ["馬 ", "羊 ", "猴 ", "雞 ", "狗 ", "豬 ", "鼠 ", "牛 ", "虎 ", "兔 ", "龍 ", "蛇 " ];
        LocationIdx = [ "南", "東", "北", "西" ];

        var func = sign ? function(d) {
                function getBit(m, n) { return (m >> n) & 1; }
                var mons = "正二三四五六七八九十冬腊", Cal = [0x41A95,0xD4A,0xDA5,0x20B55,0x56A,0x7155B,0x25D,0x92D,0x5192B,0xA95,0xB4A,0x416AA,0xAD5,0x90AB5,0x4BA,0xA5B,0x60A57,0x52B,0xA93,0x40E95];
                var total, m, n, k, isEnd = false, t = d.getYear();
                if (t < 1900) t += 1900;
                total = (t - 2001) * 365 + Math.floor((t - 2001) / 4) + [0,31,59,90,120,151,181,212,243,273,304,334][d.getMonth()] + d.getDate() - 23;
                if(d.getYear() % 4 == 0 && d.getMonth() > 1) total++;
                for(m = 0; m < 1000; m++){
                    k = (Cal[m] < 0xfff) ? 11 : 12;
                    for(n = k; n >= 0; n--) {
                        if(total <= 29 + getBit(Cal[m],n)){ 
                            isEnd=true;
                            break;
                        }
                        total = total - 29 - getBit(Cal[m],n);
                    }
                    if(isEnd) break;
                }
                var cYear = 2001 + m, cMonth = k - n + 1;
                if(k == 12){ 
                    if(cMonth == Math.floor(Cal[m] / 0x10000) + 1)
                        cMonth = 1 - cMonth;
                    if(cMonth > Math.floor(Cal[m] / 0x10000) + 1) 
                        cMonth--;  
                }
                var t = "";
                if(cMonth < 1){
                    t += "闰";
                    t += mons.charAt(-cMonth -1);
                } 
                else t += mons.charAt(cMonth - 1);
                t += "月";
                t += (total < 11) ? "初" : ((total < 20) ? "十" :( (total<30) ? "廿" : "卅"));
                if(total % 10 != 0 || total == 10) t += "一二三四五六七八九十".charAt((total-1) % 10);
                return t;        
        } : function(d) {
                var LunarYear = d.getFullYear(),
                    LunarMonth = d.getMonth() + 1,
                    LunarDate = d.getDate(),
                    acc = 0,
                    leap, SolarDate, y, im, lm;
                if (LunarYear < FIRSTYEAR || LunarYear >= LASTYEAR) {
                    alert('只处理1936 - 2031有效年份');
                    return false;
                }
                y = LunarYear - FIRSTYEAR;
                im = LunarCal[y].Intercalation;
                lm = LunarMonth;
                if (lm < 0) {
                    if (lm != -im) {
                        alert('月份无效');
                        return false;
                    } 
                } 
                else if (lm < 1 || lm > 12) {
                    alert('月份无效');
                    return false;
                } 
                if (im != 0) {
                    if (lm > im) {
                        lm++;
                    } else if (lm == -im) {
                        lm = im + 1;
                    } 
                } 
                lm--;

                if (LunarDate > LunarCal[y].MonthDays[lm] + 29) {
                    alert('农历日期不正确');
                    return false;
                }

                for (i = 0; i < lm; i++) {
                    acc += LunarCal[y].MonthDays[i] + 29;
                } 
                acc += LunarCal[y].BaseDays + LunarDate;

                leap = GetLeap(LunarYear);
                for (i = 13; i >= 0; i--) {
                    if (acc > SolarDays[leap * 14 + i])
                        break;
                }
                SolarDate = acc - SolarDays[leap * 14 + i];

                if (i <= 11) {
                    SolarYear = LunarYear;
                    SolarMonth = i + 1;
                } else {
                    SolarYear = LunarYear + 1;
                    SolarMonth = i - 11;
                }

                leap = GetLeap(SolarYear);
                y = SolarYear - FIRSTYEAR;

                acc = SolarDays[leap * 14 + SolarMonth - 1] + SolarDate;

                weekday = (acc + LunarCal[y].BaseWeekDay) % 7;
                kc = acc + LunarCal[y].BaseKanChih;
                kan = kc % 10;
                chih = kc % 12;
                return SolarYear + '-' + SolarMonth + '-' + SolarDate;            
        }

    return func(date);
}

alert(CalConvert(new Date(2010,3,19), false));  // 2010年农历四月十九  -> 2010-06-01
alert(CalConvert(new Date(2010,5,1), true));    // 2010-06-01 -> 2010年四月十九

最近要用到节气的计算.所以又扩展了一下

/**
 * 计算农历
 * @param {Date} 日期
 * @return {string} [2009,腊月,十二,(是否闰月true/false),己丑,戊寅,丙子,立秋(没有则为空)]
 */
function lunar(t){
    var __nongli = {
        L: [0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0],
        Jq: [[19, 19, 19, 21, 23, 24, 27, 29, 1, 1, 30, 30], [29, 30, 30, 2, 4, 6, 8, 10, 11, 12, 12, 11], [12, 11, 12, 13, 14, 17, 18, 20, 22, 22, 23, 22], [23, 23, 23, 24, 26, 27, 30, 2, 3, 3, 4, 4], [4, 4, 4, 6, 7, 8, 11, 12, 14, 15, 14, 15], [14, 15, 15, 17, 18, 20, 22, 23, 25, 25, 26, 25], [26, 25, 27, 27, 29, 2, 3, 4, 6, 6, 7, 6], [7, 7, 8, 8, 11, 12, 14, 16, 17, 17, 17, 18], [17, 17, 18, 20, 21, 23, 26, 27, 29, 29, 28, 28], [28, 28, 28, 1, 2, 5, 7, 8, 10, 10, 10, 10], [10, 9, 9, 11, 13, 15, 17, 19, 20, 21, 21, 21], [21, 21, 21, 22, 24, 26, 27, 1, 1, 2, 2]],
        G: ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"],
        Z: ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"],
        M: ["正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊"],
        TM: "0|gd4|wrn|1d98|1tuh|2akm|2rfn|38g9|3plp|46vz|4o9k|55px|5n73|64o5|6m37|73fd|7kna|81qe|8io7|8zgq|9g4b|9wnk|ad3g|ath2|".split('|'),
        TN: "小寒|大寒|立春|雨水|惊蛰|春分|清明|谷雨|立夏|小满|芒种|夏至|小暑|大暑|立秋|处暑|白露|秋分|寒露|霜降|立冬|小雪|大雪|冬至".split('|'),
        _GT: function(y, m, d) {
            var T = this, _tm = T.TM, _tn = T.TN;
            for (var i = 24; i--;) _tm[i] = parseInt(_tm[i], 36); 
            function term(y, n){
                var d=new Date((31556925974.7*(y-1900)+T.TM[n]*60000)+Date.UTC(1900,0,6,2,5) );
                return {m:d.getUTCMonth(),d:d.getUTCDate()}
            };
            for (var i=T.TN.length,x;i--;){
                x=term(y,i);
                if(x.m==m-1&&x.d==d)return T.TN[i];
            };
            return '';            
        },
        _ZG: function(n){
            return (this.G[n % 10] + this.Z[n % 12]);
        },
        _Y: function(y){
            var i, sum = 348;
            for (i = 0x8000; i > 0x8; i >>= 1) {
                sum += (this.L[y - 1900] & i) ? 1 : 0;
            }
            return (sum + this._D(y));
        },
        _D: function(y){
            if (this._M(y)) {
                return ((this.L[y - 1900] & 0x10000) ? 30 : 29);
            }
            else {
                return (0);
            }
        },
        _M: function(y){
            return (this.L[y - 1900] & 0xf);
        },
        _mD: function(y, m){
            return ((this.L[y - 1900] & (0x10000 >> m)) ? 30 : 29);
        },
        _lun: function(t){
            var i, leap = 0, temp = 0, T = this, ofs = Math.floor((t.getTime() + 2206425600000) / 86400000);
            var dayCyl = ofs + 40, monCyl = 14;
            var _yy = t.getFullYear(), _mm = t.getMonth()+1, _dd = t.getDate();
            for (i = 1900; i < 2050 && ofs > 0; i++) {
                temp = T._Y(i);
                ofs -= temp;
                monCyl += 12;
            }
            if (ofs < 0) {
                ofs += temp;
                i--;
                monCyl -= 12;
            }
            year = i;
            yearCyl = i - 1864;
            leap = T._M(i);
            isLeap = false;
            for (i = 1; i < 13 && ofs > 0; i++) {
                if (leap > 0 && i == (leap + 1) && isLeap == false) {
                    --i;
                    isLeap = true;
                    temp = T._D(year);
                }
                else {
                    temp = T._mD(year, i);
                }
                if (isLeap == true && i == (leap + 1)) 
                    isLeap = false;
                ofs -= temp;
                if (isLeap == false) 
                    monCyl++;
            }
            if (ofs == 0 && leap > 0 && i == leap + 1) {
                if (isLeap) {
                    isLeap = false;
                }
                else {
                    isLeap = true;
                    --i;
                    --monCyl;
                }
            }
            if (ofs < 0) 
                ofs += temp;
            --i;
            --monCyl;
            day = ofs + 1, month = i;
            var tday = (day < 11) ? "初" : ((day < 20) ? "十" : ((day < 30) ? "廿" : "卅"));
            if (day % 10 != 0 || day == 10) 
                tday += "一二三四五六七八九十".charAt((day - 1) % 10);
            var _k = (day == 1) ? (month + 1) : month, k = T.Jq[year - 1999][_k], l = T.Jq[year - 1999][_k + 1], _m = (k - l);
            if (_m < 0) 
                _m = -_m;
            if ((day >= k && parseInt(_m) < 20) || day == 1) 
                monCyl += 1;
            return [year, T.M[_k - 1] + "月", tday, isLeap, T._ZG(yearCyl), T._ZG(monCyl), T._ZG(dayCyl), T._GT(_yy, _mm, _dd)];
        }
    }
    return __nongli._lun(t);
}

关于节气的计算:

农历中一种日月兼顾的历法, 
月份是以月为历, 
节气是以日为历, 
地公转轨道是一个以太阳为一个焦点的椭圆, 
以近日点为0度,以太阳为中心, 
将地球公转所扫过的角度每十五度的界点所在日期便是一个节气。 
(可以运用立体几何算出,春分秋分大阳直射赤道,地球任意一经纬处日夜时间相等;夏至冬至太阳直射回归线[假设地球是个圆球]) 
以上是基本知识,再分析一下代码。 


代码1。 
var   sTermInfo   =   new Array(0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758) 
//每年的正小寒点到各节期正节期点(即十五度倍数点)的分种数。 
//地球公转每年都一样 
//由于公转轨道是椭圆,故这个数列并不是准确的等差数列 


代码2 
//=====   某年的第n个节气为几日(从0小寒起算) 
function   sTerm(y,n)   { 
      var   offDate   =   new   Date(   (   31556925974.7*(y-1900)   +   sTermInfo[n]*60000     )   +   Date.UTC(1900,0,6,2,5)   ) 
      return(offDate.getUTCDate()) 


31556925974.7为地球公转周期,是毫秒。(如果换算成天,是265.242199天,所以每四百年就有97个闰年,同样,我们也可以看到,如果这样做的话,一百三十万年后,我们的公历算法将有一天的误差) 

(   31556925974.7*(y-1900)   +   sTermInfo[n]*60000     )----表示y年的第n个节气点(15倍数度点)距1900年的小寒点的毫秒数。 
Date.UTC(1900,0,6,2,5)----表示1900年一年六日两点五分是正小寒点。 
以上两者相加得到的是:y年的第n个节气点时点的int值 

var   offDate   =   new   Date(   (   31556925974.7*(y-1900)   +   sTermInfo[n]*60000     )   +   Date.UTC(1900,0,6,2,5)   )----得出毫秒对应的日期 

 

独立提取的节气计算程序:

var sTermMap = '0|gd4|wrn|1d98|1tuh|2akm|2rfn|38g9|3plp|46vz|4o9k|55px|5n73|64o5|6m37|73fd|7kna|81qe|8io7|8zgq|9g4b|9wnk|ad3g|ath2|'.split('|');
var sTermNames ="小寒|大寒|立春|雨水|惊蛰|春分|清明|谷雨|立夏|小满|芒种|夏至|小暑|大暑|立秋|处暑|白露|秋分|寒露|霜降|立冬|小雪|大雪|冬至".split('|');

for (var i = 24; i--;) sTermMap[i] = parseInt(sTermMap[i], 36); 
function getTerm(y,m,d){
    function term(y,n){
        var d=new Date((31556925974.7*(y-1900)+sTermMap[n]*60000)+Date.UTC(1900,0,6,2,5) );
        return {m:d.getUTCMonth(),d:d.getUTCDate()}
    };
    for (var i=sTermNames.length,x;i--;){
        x=term(y,i);
        if(x.m==m-1&&x.d==d)return sTermNames[i];
    };
    return '';    
};

转载于:https://www.cnblogs.com/bluedream2009/archive/2010/06/02/1750359.html

你可能感兴趣的:(农历 公历 互转函数(包括节气获取))