| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2005 人关注过本帖
标题:公历农历互转
只看楼主 加入收藏
leeqihero
Rank: 3Rank: 3
等 级:论坛游侠
威 望:7
帖 子:122
专家分:135
注 册:2016-3-24
结帖率:20%
收藏
 问题点数:0 回复次数:1 
公历农历互转
程序代码:
String.prototype.padEnd=function(n,s){
    var a=n-this.length;
    return this+s.repeat(a);
};
String.prototype.padStart=function(n,s){
    var a=n-this.length;
    return s.repeat(a)+this;
};
/**用法

 * Lunar.toSolar(2016, 6, 3); 农历转化公历

 * Lunar.toLunar(2016, 7, 6); 公历转化农历

 */
var Lunar = {
    MIN_YEAR : 1891,
    MAX_YEAR : 2100,
    lunarInfo : [
        [0,2,9, 21936], [6,1,30, 9656], [0,2,17, 9584], [0,2,6, 21168], [5,1,26,43344], [0,2,13,59728],
        [0,2,2, 27296], [3,1,22,44368], [0,2,10,43856], [8,1,30,19304], [0,2,19,19168], [0,2,8, 42352],
        [5,1,29,21096], [0,2,16,53856], [0,2,4, 55632], [4,1,25,27304], [0,2,13,22176], [0,2,2, 39632],
        [2,1,22,19176], [0,2,10,19168], [6,1,30,42200], [0,2,18,42192], [0,2,6, 53840], [5,1,26,54568],
        [0,2,14,46400], [0,2,3, 54944], [2,1,23,38608], [0,2,11,38320], [7,2,1, 18872], [0,2,20,18800],
        [0,2,8, 42160], [5,1,28,45656], [0,2,16,27216], [0,2,5, 27968], [4,1,24,44456], [0,2,13,11104],
        [0,2,2, 38256], [2,1,23,18808], [0,2,10,18800], [6,1,30,25776], [0,2,17,54432], [0,2,6, 59984],
        [5,1,26,27976], [0,2,14,23248], [0,2,4, 11104], [3,1,24,37744], [0,2,11,37600], [7,1,31,51560],
        [0,2,19,51536], [0,2,8, 54432], [6,1,27,55888], [0,2,15,46416], [0,2,5, 22176], [4,1,25,43736],
        [0,2,13, 9680], [0,2,2, 37584], [2,1,22,51544], [0,2,10,43344], [7,1,29,46248], [0,2,17,27808],
        [0,2,6, 46416], [5,1,27,21928], [0,2,14,19872], [0,2,3, 42416], [3,1,24,21176], [0,2,12,21168],
        [8,1,31,43344], [0,2,18,59728], [0,2,8, 27296], [6,1,28,44368], [0,2,15,43856], [0,2,5, 19296],
        [4,1,25,42352], [0,2,13,42352], [0,2,2, 21088], [3,1,21,59696], [0,2,9, 55632], [7,1,30,23208],
        [0,2,17,22176], [0,2,6, 38608], [5,1,27,19176], [0,2,15,19152], [0,2,3, 42192], [4,1,23,53864],
        [0,2,11,53840], [8,1,31,54568], [0,2,18,46400], [0,2,7, 46752], [6,1,28,38608], [0,2,16,38320],
        [0,2,5, 18864], [4,1,25,42168], [0,2,13,42160], [10,2,2,45656], [0,2,20,27216], [0,2,9, 27968],
        [6,1,29,44448], [0,2,17,43872], [0,2,6, 38256], [5,1,27,18808], [0,2,15,18800], [0,2,4, 25776],
        [3,1,23,27216], [0,2,10,59984], [8,1,31,27432], [0,2,19,23232], [0,2,7, 43872], [5,1,28,37736],
        [0,2,16,37600], [0,2,5, 51552], [4,1,24,54440], [0,2,12,54432], [0,2,1, 55888], [2,1,22,23208],
        [0,2,9, 22176], [7,1,29,43736], [0,2,18, 9680], [0,2,7, 37584], [5,1,26,51544], [0,2,14,43344],
        [0,2,3, 46240], [4,1,23,46416], [0,2,10,44368], [9,1,31,21928], [0,2,19,19360], [0,2,8, 42416],
        [6,1,28,21176], [0,2,16,21168], [0,2,5, 43312], [4,1,25,29864], [0,2,12,27296], [0,2,1, 44368],
        [2,1,22,19880], [0,2,10,19296], [6,1,29,42352], [0,2,17,42208], [0,2,6, 53856], [5,1,26,59696],
        [0,2,13,54576], [0,2,3, 23200], [3,1,23,27472], [0,2,11,38608], [11,1,31,19176],[0,2,19,19152],
        [0,2,8, 42192], [6,1,28,53848], [0,2,15,53840], [0,2,4, 54560], [5,1,24,55968], [0,2,12,46496],
        [0,2,1, 22224], [2,1,22,19160], [0,2,10,18864], [7,1,30,42168], [0,2,17,42160], [0,2,6, 43600],
        [5,1,26,46376], [0,2,14,27936], [0,2,2, 44448], [3,1,23,21936], [0,2,11,37744], [8,2,1, 18808],
        [0,2,19,18800], [0,2,8, 25776], [6,1,28,27216], [0,2,15,59984], [0,2,4, 27424], [4,1,24,43872],
        [0,2,12,43744], [0,2,2, 37600], [3,1,21,51568], [0,2,9, 51552], [7,1,29,54440], [0,2,17,54432],
        [0,2,5, 55888], [5,1,26,23208], [0,2,14,22176], [0,2,3, 42704], [4,1,23,21224], [0,2,11,21200],
        [8,1,31,43352], [0,2,19,43344], [0,2,7, 46240], [6,1,27,46416], [0,2,15,44368], [0,2,5, 21920],
        [4,1,24,42448], [0,2,12,42416], [0,2,2, 21168], [3,1,22,43320], [0,2,9, 26928], [7,1,29,29336],
        [0,2,17,27296], [0,2,6, 44368], [5,1,26,19880], [0,2,14,19296], [0,2,3, 42352], [4,1,24,21104],
        [0,2,10,53856], [8,1,30,59696], [0,2,18,54560], [0,2,7, 55968], [6,1,27,27472], [0,2,15,22224],
        [0,2,5, 19168], [4,1,25,42216], [0,2,12,42192], [0,2,1, 53584], [2,1,21,55592], [0,2,9, 54560]
    ],
    //是否闰年
    isLeapYear : function(year) {
        return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
    },
    //天干地支年
    lunarYear : function(year) {
        var gan = '庚辛壬癸甲乙丙丁戊己',
            zhi = '申酉戌亥子丑寅卯辰巳午未';
        return gan[year%10]+zhi[year%12];
    },
    //生肖年
    zodiacYear : function(year) {
        var zodiac = '猴鸡狗猪鼠牛虎兔龙蛇马羊';
        return zodiac[year % 12];
    },
    //公历月份天数
    //@param year 阳历-年
    //@param month 阳历-月
    solarMonthDays : function(year, month) {
        var FebDays = this.isLeapYear(year) ? 29 : 28;
        var monthHash = ['', 31, FebDays, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        return monthHash[month];
    },
    //农历月份天数
    lunarMonthDays : function(year, month) {
        var monthData = this.lunarMonths(year);
        return monthData[month - 1];
    },
    //农历月份天数数组
    lunarMonths : function(year) {
        var yearData = this.lunarInfo[year - this.MIN_YEAR];
        var leapMonth = yearData[0];
        var bit = yearData[3].toString(2);
        return bit.padStart(16,'0').slice(0,(leapMonth?13:12)).split('').map(c=>c*1+29);
    },
    //农历每年的天数
    //@param year 农历年份
    lunarYearDays : function(year) {
        var monthArray = this.lunarYearMonths(year);
        var len = monthArray.length;
        return (monthArray[len-1] == 0 ? monthArray[len-2] : monthArray[len-1]);
    },
    //
    lunarYearMonths : function(year) {
        var monthData = this.lunarMonths(year);
        var res = [];
        var temp = 0;
        var yearData = this.lunarInfo[year - this.MIN_YEAR];
        var len = (yearData[0]?13:12);
        for(var i=0;i<len;i++){
            temp+=monthData[i];
            res.push(temp);
        };
        return res;
    },
    //获取闰月
    //@param year 农历年份
    leapMonth : function(year){
        var yearData = this.lunarInfo[year - this.MIN_YEAR];
        return yearData[0];
    },
    //计算农历日期与正月初一相隔的天数
    betweenLunarDays : function(year, month, day) {
        var yearMonth = this.lunarMonths(year);
        var res = 0;
        for (var i = 1; i < month; i++) {
            res += yearMonth[i-1];
        }
        res += day - 1;
        return res;
    },
    //计算2个阳历日期之间的天数
    //@param year 阳历年
    //@param month
    //@param day
    //@param l_month 阴历正月对应的阳历月份
    //@param l_day   阴历初一对应的阳历天
    betweenSolarDays : function(year, month, day, l_month, l_day) {
        var time1 = new Date(year +"-"+  month  +"-"+ day).getTime(),
            time2 = new Date(year +"-"+ l_month +"-"+ l_day).getTime();
        return Math.ceil((time1-time2)/24/3600/1000);
    },
    //中文月份
    chineseMonth : function(month) {
        var monthHash = '*一二三四五六七八九十冬腊';
        return monthHash[month]+'月';
    },
    //根据距离正月初一的天数计算阴历日期
    //@param year 阳历年
    //@param between 天数
    lunarByBetween : function(year, between) {
        var lunarArray = [], yearMonth = [], t = 0, e = 0, leapMonth = 0, m = '';
        if (between == 0) {
            t = 1;
            e = 1;
            m = '正月';
        } else {
            year = between > 0 ? year : (year - 1);
            yearMonth = this.lunarYearMonths(year);
            leapMonth = this.leapMonth(year);
            between   = between > 0 ? between : (this.lunarYearDays(year) + between);
            for (var i = 0; i < 13; i++) {
                if (between == yearMonth[i]) {
                    t = i + 2;
                    e = 1;
                    break;
                } else if (between < yearMonth[i]) {
                    t = i + 1;
                    e = between - ((yearMonth[i-1]) ? yearMonth[i-1] : 0) + 1;
                    break;
                }
            }
             
            m = (leapMonth != 0 && t == leapMonth + 1)
            ? ('闰'+this.chineseMonth(t-1))
            : this.chineseMonth(((leapMonth != 0 && leapMonth + 1 < t) ? (t - 1) : t));
        }
        lunarArray.push(year, t, e); //年 月 日
        lunarArray.push(this.lunarYear(year),
                        this.zodiacYear(year),
                        m,
                        this.chineseNumber(e)); //天干地支年 生肖年 月份 日
        lunarArray.push(leapMonth); //闰几月
        return lunarArray;
    },
    //中文日期
    chineseNumber : function(num) {
        var dateHash = '*一二三四五六七八九十';
        if (num <= 10) {
            res = '初'+ dateHash[num];
        } else if (num > 10 && num < 20) {
            res = '十'+ dateHash[num-10];
        } else if (num == 20) {
            res = "二十";
        } else if (num > 20 && num < 30) {
            res = "廿"+ dateHash[num-20];
        } else if (num == 30) {
            res = "三十";
        }
        return res;
    },
    //转换农历
    toLunar : function(year, month, day) {
        var yearData = this.lunarInfo[year - this.MIN_YEAR];
        if (year == this.MIN_YEAR && month <= 2 && day <= 9) {
            return [1891, 1, 1, '辛卯', '兔', '正月', '初一'];
        }
        return this.lunarByBetween(year, this.betweenSolarDays(year, month, day, yearData[1], yearData[2]));
    },
    //转换公历
    //@param year  阴历-年
    //@param month 阴历-月,闰月处理:例如如果当年闰五月,那么第二个五月就传六月,相当于阴历有13个月
    //@param date  阴历-日
    toSolar : function(year, month, day) {
        var yearData = this.lunarInfo[year - this.MIN_YEAR];
        var between  = this.betweenLunarDays(year, month, day);
        var ms = new Date(year +"-" + yearData[1] +"-"+ yearData[2]).getTime();
        var s = ms + between * 24 * 60 * 60 * 1000;
        var d = new Date();
        d.setTime(s);
        year  = d.getFullYear();
        month = d.getMonth() + 1;
        day   = d.getDate();
        return [year, month, day];
    }
};
搜索更多相关主题的帖子: var return year this function 
2020-06-26 18:41
leeqihero
Rank: 3Rank: 3
等 级:论坛游侠
威 望:7
帖 子:122
专家分:135
注 册:2016-3-24
收藏
得分:0 
程序代码:
/**公历转化农历用法
* toLunar('2020-6-7'); //["庚子年", "闰四月", "十六"]
*/
toLunar=function(date){
    var lunarInfo=[
        [0,'1891-2-9','010101011011'],[6,'1892-1-30','0010010110111'],[0,'1893-2-17','001001010111'],
        [0,'1894-2-6','010100101011'],[5,'1895-1-26','1010100101010'],[0,'1896-2-13','111010010101'],
        [0,'1897-2-2','011010101010'],[3,'1898-1-22','1010110101010'],[0,'1899-2-10','101010110101'],
        [8,'1900-1-30','0100101101101'],[0,'1901-2-19','010010101110'],[0,'1902-2-8','101001010111'],
        [5,'1903-1-29','0101001001101'],[0,'1904-2-16','110100100110'],[0,'1905-2-4','110110010101'],
        [4,'1906-1-25','0110101010101'],[0,'1907-2-13','010101101010'],[0,'1908-2-2','100110101101'],
        [2,'1909-1-22','0100101011101'],[0,'1910-2-10','010010101110'],[6,'1911-1-30','1010010011011'],
        [0,'1912-2-18','101001001101'],[0,'1913-2-6','110100100101'],[5,'1914-1-26','1101010100101'],
        [0,'1915-2-14','101101010100'],[0,'1916-2-3','110101101010'],[2,'1917-1-23','1001011011010'],
        [0,'1918-2-11','100101011011'],[7,'1919-2-1','0100100110111'],[0,'1920-2-20','010010010111'],
        [0,'1921-2-8','101001001011'],[5,'1922-1-28','1011001001011'],[0,'1923-2-16','011010100101'],
        [0,'1924-2-5','011011010100'],[4,'1925-1-24','1010110110101'],[0,'1926-2-13','001010110110'],
        [0,'1927-2-2','100101010111'],[2,'1928-1-23','0100100101111'],[0,'1929-2-10','010010010111'],
        [6,'1930-1-30','0110010010110'],[0,'1931-2-17','110101001010'],[0,'1932-2-6','111010100101'],
        [5,'1933-1-26','0110110101001'],[0,'1934-2-14','010110101101'],[0,'1935-2-4','001010110110'],
        [3,'1936-1-24','1001001101110'],[0,'1937-2-11','100100101110'],[7,'1938-1-31','1100100101101'],
        [0,'1939-2-19','110010010101'],[0,'1940-2-8','110101001010'],[6,'1941-1-27','1101101001010'],
        [0,'1942-2-15','101101010101'],[0,'1943-2-5','010101101010'],[4,'1944-1-25','1010101011011'],
        [0,'1945-2-13','001001011101'],[0,'1946-2-2','100100101101'],[2,'1947-1-22','1100100101011'],
        [0,'1948-2-10','101010010101'],[7,'1949-1-29','1011010010101'],[0,'1950-2-17','011011001010'],
        [0,'1951-2-6','101101010101'],[5,'1952-1-27','0101010110101'],[0,'1953-2-14','010011011010'],
        [0,'1954-2-3','101001011011'],[3,'1955-1-24','0101001010111'],[0,'1956-2-12','010100101011'],
        [8,'1957-1-31','1010100101010'],[0,'1958-2-18','111010010101'],[0,'1959-2-8','011010101010'],
        [6,'1960-1-28','1010110101010'],[0,'1961-2-15','101010110101'],[0,'1962-2-5','010010110110'],
        [4,'1963-1-25','1010010101110'],[0,'1964-2-13','101001010111'],[0,'1965-2-2','010100100110'],
        [3,'1966-1-21','1110100100110'],[0,'1967-2-9','110110010101'],[7,'1968-1-30','0101101010101'],
        [0,'1969-2-17','010101101010'],[0,'1970-2-6','100101101101'],[5,'1971-1-27','0100101011101'],
        [0,'1972-2-15','010010101101'],[0,'1973-2-3','101001001101'],[4,'1974-1-23','1101001001101'],
        [0,'1975-2-11','110100100101'],[8,'1976-1-31','1101010100101'],[0,'1977-2-18','101101010100'],
        [0,'1978-2-7','101101101010'],[6,'1979-1-28','1001011011010'],[0,'1980-2-16','100101011011'],
        [0,'1981-2-5','010010011011'],[4,'1982-1-25','1010010010111'],[0,'1983-2-13','101001001011'],
        [10,'1984-2-2','1011001001011'],[0,'1985-2-20','011010100101'],[0,'1986-2-9','011011010100'],
        [6,'1987-1-29','1010110110100'],[0,'1988-2-17','101010110110'],[0,'1989-2-6','100101010111'],
        [5,'1990-1-27','0100100101111'],[0,'1991-2-15','010010010111'],[0,'1992-2-4','011001001011'],
        [3,'1993-1-23','0110101001010'],[0,'1994-2-10','111010100101'],[8,'1995-1-31','0110101100101'],
        [0,'1996-2-19','010110101100'],[0,'1997-2-7','101010110110'],[5,'1998-1-28','1001001101101'],
        [0,'1999-2-16','100100101110'],[0,'2000-2-5','110010010110'],[4,'2001-1-24','1101010010101'],
        [0,'2002-2-12','110101001010'],[0,'2003-2-1','110110100101'],[2,'2004-1-22','0101101010101'],
        [0,'2005-2-9','010101101010'],[7,'2006-1-29','1010101011011'],[0,'2007-2-18','001001011101'],
        [0,'2008-2-7','100100101101'],[5,'2009-1-26','1100100101011'],[0,'2010-2-14','101010010101'],
        [0,'2011-2-3','101101001010'],[4,'2012-1-23','1011010101010'],[0,'2013-2-10','101011010101'],
        [9,'2014-1-31','0101010110101'],[0,'2015-2-19','010010111010'],[0,'2016-2-8','101001011011'],
        [6,'2017-1-28','0101001010111'],[0,'2018-2-16','010100101011'],[0,'2019-2-5','101010010011'],
        [4,'2020-1-25','0111010010101'],[0,'2021-2-12','011010101010'],[0,'2022-2-1','101011010101'],
        [2,'2023-1-22','0100110110101'],[0,'2024-2-10','010010110110'],[6,'2025-1-29','1010010101110'],
        [0,'2026-2-17','101001001110'],[0,'2027-2-6','110100100110'],[5,'2028-1-26','1110100100110'],
        [0,'2029-2-13','110101010011'],[0,'2030-2-3','010110101010'],[3,'2031-1-23','0110101101010'],
        [0,'2032-2-11','100101101101'],[11,'2033-1-31','0100101011101'],[0,'2034-2-19','010010101101'],
        [0,'2035-2-8','101001001101'],[6,'2036-1-28','1101001001011'],[0,'2037-2-15','110100100101'],
        [0,'2038-2-4','110101010010'],[5,'2039-1-24','1101101010100'],[0,'2040-2-12','101101011010'],
        [0,'2041-2-1','010101101101'],[2,'2042-1-22','0100101011011'],[0,'2043-2-10','010010011011'],
        [7,'2044-1-30','1010010010111'],[0,'2045-2-17','101001001011'],[0,'2046-2-6','101010100101'],
        [5,'2047-1-26','1011010100101'],[0,'2048-2-14','011011010010'],[0,'2049-2-2','101011011010'],
        [3,'2050-1-23','0101010110110'],[0,'2051-2-11','100100110111'],[8,'2052-2-1','0100100101111'],
        [0,'2053-2-19','010010010111'],[0,'2054-2-8','011001001011'],[6,'2055-1-28','0110101001010'],
        [0,'2056-2-15','111010100101'],[0,'2057-2-4','011010110010'],[4,'2058-1-24','1010101101100'],
        [0,'2059-2-12','101010101110'],[0,'2060-2-2','100100101110'],[3,'2061-1-21','1100100101110'],
        [0,'2062-2-9','110010010110'],[7,'2063-1-29','1101010010101'],[0,'2064-2-17','110101001010'],
        [0,'2065-2-5','110110100101'],[5,'2066-1-26','0101101010101'],[0,'2067-2-14','010101101010'],
        [0,'2068-2-3','101001101101'],[4,'2069-1-23','0101001011101'],[0,'2070-2-11','010100101101'],
        [8,'2071-1-31','1010100101011'],[0,'2072-2-19','101010010101'],[0,'2073-2-7','101101001010'],
        [6,'2074-1-27','1011010101010'],[0,'2075-2-15','101011010101'],[0,'2076-2-5','010101011010'],
        [4,'2077-1-24','1010010111010'],[0,'2078-2-12','101001011011'],[0,'2079-2-2','010100101011'],
        [3,'2080-1-22','1010100100111'],[0,'2081-2-9','011010010011'],[7,'2082-1-29','0111001010011'],
        [0,'2083-2-17','011010101010'],[0,'2084-2-6','101011010101'],[5,'2085-1-26','0100110110101'],
        [0,'2086-2-14','010010110110'],[0,'2087-2-3','101001010111'],[4,'2088-1-24','0101001001110'],
        [0,'2089-2-10','110100100110'],[8,'2090-1-30','1110100100110'],[0,'2091-2-18','110101010010'],
        [0,'2092-2-7','110110101010'],[6,'2093-1-27','0110101101010'],[0,'2094-2-15','010101101101'],
        [0,'2095-2-5','010010101110'],[4,'2096-1-25','1010010011101'],[0,'2097-2-12','101001001101'],
        [0,'2098-2-1','110100010101'],[2,'2099-1-21','1101100100101'],[0,'2100-2-9','110101010010']
    ];
    var MIN_YEAR=1891;
    var year=date.split('-')[0]*1;
    var yearData=lunarInfo[year-MIN_YEAR];
    var lunarMonths=yearData[2].split('').map(c=>c*1+29);
    var between=Math.ceil((new Date(date).getTime()-new Date(yearData[1]).getTime())/24/3600/1000);
    if(between<0){
        year--;
        between+=lunarMonths.reduce((p,c)=>p+c,0);
    };
    var mmm='正二三四五六七八九十冬腊';
    var MMM=mmm.split('');
    if(yearData[0]){
        MMM.splice(yearData[0],0,'闰'+mmm[yearData[0]-1]);
    };
    var t=0;
    //农历月份天数累计数组
    var LM=lunarMonths.map(c=>{
        t+=c;
        return t;
    });
    var index=LM.findIndex(c=>c>between);
    return ['庚辛壬癸甲乙丙丁戊己'[year%10]+'申酉戌亥子丑寅卯辰巳午未'[year%12]+'年',MMM[index]+'月','初一,初二,初三,初四,初五,初六,初七,初八,初九,初十,十一,十二,十三,十四,十五,十六,十七,十八,十九,二十,廿一,廿二,廿三,廿四,廿五,廿六,廿七,廿八,廿九,三十'.split(',')[index?between-LM[index-1]:between]];
};
2020-07-17 18:06
快速回复:公历农历互转
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036809 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved