The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

用 C# 開發農曆(陰曆)計算程式

天啊,農曆好複雜阿!

剛剛弄了好久,加了好多「看的懂」的註解,才把我要的功能寫出來!

現在眼睛酸、肚子餓、不想說話了,自己看 Code 吧!最下面有一些使用範例! 

using System;
namespace CNCalendar
{
    public class CNDate
    {
        private const ushort START_YEAR = 1901;

        private const ushort END_YEAR = 2050;

        private string[] ConstellationName =
              {
              "白羊座", "金牛座", "雙子座",
              "巨蟹座", "獅子座", "處女座",
              "天秤座", "天蠍座", "射手座",
              "摩羯座", "水瓶座", "雙魚座"};

        private string[] LunarHolDayName =
              {
              "小寒", "大寒", "立春", "雨水",
              "驚蟄", "春分", "清明", "谷雨",
              "立夏", "小滿", "芒種", "夏至",
              "小暑", "大暑", "立秋", "處暑",
              "白露", "秋分", "寒露", "霜降",
              "立冬", "小雪", "大雪", "冬至"};

        // 農曆每個月的天數 ( 紀錄 1901.1.1 到 2050.12.31 的每一個月 )
        // 農曆每月只能是 29 或 30 天,一年用 12(或13)個二進制位表示,對應位元為 1 表 30 天,否則為 29 天
        private int[] gLunarMonthDay = {
              0x4ae0, 0xa570, 0x5268, 0xd260, 0xd950, 0x6aa8, 0x56a0, 0x9ad0, 0x4ae8, 0x4ae0, //1910
              0xa4d8, 0xa4d0, 0xd250, 0xd548, 0xb550, 0x56a0, 0x96d0, 0x95b0, 0x49b8, 0x49b0, //1920
              0xa4b0, 0xb258, 0x6a50, 0x6d40, 0xada8, 0x2b60, 0x9570, 0x4978, 0x4970, 0x64b0, //1930
              0xd4a0, 0xea50, 0x6d48, 0x5ad0, 0x2b60, 0x9370, 0x92e0, 0xc968, 0xc950, 0xd4a0, //1940
              0xda50, 0xb550, 0x56a0, 0xaad8, 0x25d0, 0x92d0, 0xc958, 0xa950, 0xb4a8, 0x6ca0, //1950
              0xb550, 0x55a8, 0x4da0, 0xa5b0, 0x52b8, 0x52b0, 0xa950, 0xe950, 0x6aa0, 0xad50, //1960
              0xab50, 0x4b60, 0xa570, 0xa570, 0x5260, 0xe930, 0xd950, 0x5aa8, 0x56a0, 0x96d0, //1970
              0x4ae8, 0x4ad0, 0xa4d0, 0xd268, 0xd250, 0xd528, 0xb540, 0xb6a0, 0x96d0, 0x95b0, //1980
              0x49b0, 0xa4b8, 0xa4b0, 0xb258, 0x6a50, 0x6d40, 0xada0, 0xab60, 0x9370, 0x4978, //1990
              0x4970, 0x64b0, 0x6a50, 0xea50, 0x6b28, 0x5ac0, 0xab60, 0x9368, 0x92e0, 0xc960, //2000
              0xd4a8, 0xd4a0, 0xda50, 0x5aa8, 0x56a0, 0xaad8, 0x25d0, 0x92d0, 0xc958, 0xa950, //2010
              0xb4a0, 0xb550, 0xb550, 0x55a8, 0x4ba0, 0xa5b0, 0x52b8, 0x52b0, 0xa930, 0x74a8, //2020
              0x6aa0, 0xad50, 0x4da8, 0x4b60, 0x9570, 0xa4e0, 0xd260, 0xe930, 0xd530, 0x5aa0, //2030
              0x6b50, 0x96d0, 0x4ae8, 0x4ad0, 0xa4d0, 0xd258, 0xd250, 0xd520, 0xdaa0, 0xb5a0, //2040
              0x56d0, 0x4ad8, 0x49b0, 0xa4b8, 0xa4b0, 0xaa50, 0xb528, 0x6d20, 0xada0, 0x55b0}; //2050

        // 農曆每年潤月的月份 ( 紀錄 1901.1.1 到 2050.12.31 的每一個月 )
        // 如沒有則為0,每個 byte 儲存兩年的資料
        byte[] gLunarMonth ={
              0x00, 0x50, 0x04, 0x00, 0x20, //1910
              0x60, 0x05, 0x00, 0x20, 0x70, //1920
              0x05, 0x00, 0x40, 0x02, 0x06, //1930
              0x00, 0x50, 0x03, 0x07, 0x00, //1940
              0x60, 0x04, 0x00, 0x20, 0x70, //1950
              0x05, 0x00, 0x30, 0x80, 0x06, //1960
              0x00, 0x40, 0x03, 0x07, 0x00, //1970
              0x50, 0x04, 0x08, 0x00, 0x60, //1980
              0x04, 0x0a, 0x00, 0x60, 0x05, //1990
              0x00, 0x30, 0x80, 0x05, 0x00, //2000
              0x40, 0x02, 0x07, 0x00, 0x50, //2010
              0x04, 0x09, 0x00, 0x60, 0x04, //2020
              0x00, 0x20, 0x60, 0x05, 0x00, //2030
              0x30, 0xb0, 0x06, 0x00, 0x50, //2040
              0x02, 0x07, 0x00, 0x50, 0x03}; //2050

        // 位元陣列 gLanarHoliDay 存放每年的二十四節氣對應的陽曆日期
        // 每年的二十四節氣對應的陽曆日期幾乎固定,平均分佈於十二個月中
        // 1月 2月 3月 4月 5月 6月
        // 小寒 大寒 立春 雨水 驚蟄 春分 清明 谷雨 立夏 小滿 芒種 夏至
        // 7月 8月 9月 10月 11月 12月
        // 小暑 大暑 立秋 處暑 白露 秋分 寒露 霜降 立冬 小雪 大雪 冬至
        //*********************************************************************************
        // 節氣無任何確定規律,所以只好建立表格對應
        //**********************************************************************************}
        // 數據格式說明:
        // 如 1901 年的節氣為
        // 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
        // 6, 21, 4, 19, 6, 21, 5, 21, 6,22, 6,22, 8, 23, 8, 24, 8, 24, 8, 24, 8, 23, 8, 22
        // 9, 6, 11,4, 9, 6, 10,6, 9,7, 9,7, 7, 8, 7, 9, 7, 9, 7, 9, 7, 8, 7, 15
        // 上面第一行數據為每月節氣對應日期,15減去每月第一個節氣,每月第二個節氣減去15得第二行
        // 這樣每月兩個節氣對應數據都小於16,每月用一個陣列元素存放
        // 高位元(high-bit)存放第一個節氣數據,低位元(low-bit)存放第二個節氣的數據,可得下表
        byte[] gLunarHoliDay ={
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1901
              0x96, 0xA4, 0x96, 0x96, 0x97, 0x87, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1902
              0x96, 0xA5, 0x87, 0x96, 0x87, 0x87, 0x79, 0x69, 0x69, 0x69, 0x78, 0x78, //1903
              0x86, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x78, 0x87, //1904
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1905
              0x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1906
              0x96, 0xA5, 0x87, 0x96, 0x87, 0x87, 0x79, 0x69, 0x69, 0x69, 0x78, 0x78, //1907
              0x86, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1908
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1909
              0x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1910
              0x96, 0xA5, 0x87, 0x96, 0x87, 0x87, 0x79, 0x69, 0x69, 0x69, 0x78, 0x78, //1911
              0x86, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1912
              0x95, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1913
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1914
              0x96, 0xA5, 0x97, 0x96, 0x97, 0x87, 0x79, 0x79, 0x69, 0x69, 0x78, 0x78, //1915
              0x96, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1916
              0x95, 0xB4, 0x96, 0xA6, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x87, //1917
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x77, //1918
              0x96, 0xA5, 0x97, 0x96, 0x97, 0x87, 0x79, 0x79, 0x69, 0x69, 0x78, 0x78, //1919
              0x96, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1920
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x87, //1921
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x77, //1922
              0x96, 0xA4, 0x96, 0x96, 0x97, 0x87, 0x79, 0x79, 0x69, 0x69, 0x78, 0x78, //1923
              0x96, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1924
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x87, //1925
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1926
              0x96, 0xA4, 0x96, 0x96, 0x97, 0x87, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1927
              0x96, 0xA5, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1928
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1929
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1930
              0x96, 0xA4, 0x96, 0x96, 0x97, 0x87, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1931
              0x96, 0xA5, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1932
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1933
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1934
              0x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1935
              0x96, 0xA5, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1936
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1937
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1938
              0x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1939
              0x96, 0xA5, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1940
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1941
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1942
              0x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1943
              0x96, 0xA5, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1944
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1945
              0x95, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, //1946
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1947
              0x96, 0xA5, 0xA6, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //1948
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x79, 0x78, 0x79, 0x77, 0x87, //1949
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, //1950
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, //1951
              0x96, 0xA5, 0xA6, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //1952
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1953
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x78, 0x79, 0x78, 0x68, 0x78, 0x87, //1954
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1955
              0x96, 0xA5, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //1956
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1957
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1958
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1959
              0x96, 0xA4, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //1960
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1961
              0x96, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1962
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1963
              0x96, 0xA4, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //1964
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1965
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1966
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1967
              0x96, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //1968
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1969
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1970
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, //1971
              0x96, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //1972
              0xA5, 0xB5, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1973
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1974
              0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, //1975
              0x96, 0xA4, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x89, 0x88, 0x78, 0x87, 0x87, //1976
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //1977
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x78, 0x87, //1978
              0x96, 0xB4, 0x96, 0xA6, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, //1979
              0x96, 0xA4, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //1980
              0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x77, 0x87, //1981
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1982
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, //1983
              0x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x87, //1984
              0xA5, 0xB4, 0xA6, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //1985
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1986
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x79, 0x78, 0x69, 0x78, 0x87, //1987
              0x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, //1988
              0xA5, 0xB4, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //1989
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //1990
              0x95, 0xB4, 0x96, 0xA5, 0x86, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1991
              0x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, //1992
              0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //1993
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1994
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x76, 0x78, 0x69, 0x78, 0x87, //1995
              0x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, //1996
              0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //1997
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //1998
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //1999
              0x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, //2000
              0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //2001
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //2002
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //2003
              0x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, //2004
              0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //2005
              0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2006
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, //2007
              0x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, //2008
              0xA5, 0xB3, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //2009
              0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2010
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x78, 0x87, //2011
              0x96, 0xB4, 0xA5, 0xB5, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, //2012
              0xA5, 0xB3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x87, //2013
              0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2014
              0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //2015
              0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, //2016
              0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x87, //2017
              0xA5, 0xB4, 0xA6, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2018
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //2019
              0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x86, //2020
              0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, //2021
              0xA5, 0xB4, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //2022
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, //2023
              0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, //2024
              0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, //2025
              0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //2026
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //2027
              0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, //2028
              0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, //2029
              0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //2030
              0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, //2031
              0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, //2032
              0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x86, //2033
              0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x78, 0x88, 0x78, 0x87, 0x87, //2034
              0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2035
              0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, //2036
              0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, //2037
              0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //2038
              0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2039
              0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, //2040
              0xA5, 0xC3, 0xA5, 0xB5, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, //2041
              0xA5, 0xB3, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, //2042
              0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2043
              0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x88, 0x87, 0x96, //2044
              0xA5, 0xC3, 0xA5, 0xB4, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, //2045
              0xA5, 0xB3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x87, //2046
              0xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, //2047
              0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA5, 0x97, 0x87, 0x87, 0x88, 0x86, 0x96, //2048
              0xA4, 0xC3, 0xA5, 0xA5, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x86, //2049
              0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x78, 0x78, 0x87, 0x87}; //2050

        private DateTime m_Date;
        public DateTime Date
        {
            get { return m_Date; }
            set { m_Date = value; }
        }

        public CNDate()
        {
            Date = DateTime.Today;
        }

        public CNDate(DateTime dt)
        {
            Date = dt.Date;
        }

        /// <summary>
        /// 計算指定日期的星座序號
        /// </summary>
        /// <returns></returns>
        public int GetConstellation()
        {
            int Y, M, D;
            Y = m_Date.Year;
            M = m_Date.Month;
            D = m_Date.Day;
            Y = M * 100 + D;
            if (((Y >= 321) && (Y <= 419))) { return 0; }
            else if ((Y >= 420) && (Y <= 520)) { return 1; }
            else if ((Y >= 521) && (Y <= 620)) { return 2; }
            else if ((Y >= 621) && (Y <= 722)) { return 3; }
            else if ((Y >= 723) && (Y <= 822)) { return 4; }
            else if ((Y >= 823) && (Y <= 922)) { return 5; }
            else if ((Y >= 923) && (Y <= 1022)) { return 6; }
            else if ((Y >= 1023) && (Y <= 1121)) { return 7; }
            else if ((Y >= 1122) && (Y <= 1221)) { return 8; }
            else if ((Y >= 1222) || (Y <= 119)) { return 9; }
            else if ((Y >= 120) && (Y <= 218)) { return 10; }
            else if ((Y >= 219) && (Y <= 320)) { return 11; }
            else { return -1; };
        }

        /// <summary>
        /// 計算指定日期的星座名稱
        /// </summary>
        /// <returns></returns>
        public string GetConstellationName()
        {
            int Constellation;
            Constellation = GetConstellation();
            if ((Constellation >= 0) && (Constellation <= 11))
            { return ConstellationName[Constellation]; }
            else
            { return ""; };
        }

        /// <summary>
        /// 計算國曆當天對應的節氣 0-23,-1表示不是節氣
        /// </summary>
        /// <returns></returns>
        public int l_GetLunarHolDay()
        {
            byte Flag;
            int Day, iYear, iMonth, iDay;
            iYear = m_Date.Year;
            if ((iYear < START_YEAR) || (iYear > END_YEAR))
            { return -1; };
            iMonth = m_Date.Month;
            iDay = m_Date.Day;
            Flag = gLunarHoliDay[(iYear - START_YEAR) * 12 + iMonth - 1];
            if (iDay < 15)
            { Day = 15 - ((Flag >> 4) & 0x0f); }
            else
            { Day = (Flag & 0x0f) + 15; };
            if (iDay == Day)
            {
                if (iDay > 15)
                { return (iMonth - 1) * 2 + 1; }
                else
                { return (iMonth - 1) * 2; }
            }
            else
            { return -1; };
        }

        /// <summary>
        /// 取回格式化後的月份名稱 ( 例如:一月 => 在農曆的格式叫做「正月」 )
        /// </summary>
        /// <param name="iMonth">月份</param>
        /// <param name="bLunar">是否為農曆月份</param>
        /// <returns></returns>
        public string FormatMonth(ushort iMonth, bool bLunar)
        {
            string szText = "正二三四五六七八九十";
            string strMonth;

            if ((!bLunar) && (iMonth == 1))
            {
                return "一月";
            }

            if (iMonth <= 10)
            {
                strMonth = "";
                strMonth = strMonth + szText.Substring(iMonth - 1, 1);
                strMonth = strMonth + "月";
                return strMonth;
            }

            if (iMonth == 11)
            {
                strMonth = "十一";
            }
            else
            {
                strMonth = "十二";
            }
            return strMonth + "月";
        }

        /// <summary>
        /// 取回格式化後的農曆日期名稱 ( 例如:一日 => 在農曆的格式叫做「初一」 )
        /// </summary>
        /// <param name="iDay">農曆的日期(「日」的部分)</param>
        /// <returns></returns>
        public string FormatLunarDay(ushort iDay)
        {
            string szText1 = "初十廿三";
            string szText2 = "一二三四五六七八九十";
            string strDay;
            if ((iDay != 20) && (iDay != 30))
            {
                strDay = szText1.Substring((iDay - 1) / 10, 1);
                strDay = strDay + szText2.Substring((iDay - 1) % 10, 1);
            }
            else
            {
                strDay = szText1.Substring((iDay / 10) * 2 + 1, 2);
                strDay = strDay + "十";
            }
            return strDay;
        }

        /// <summary>
        /// 取得今年的農曆過年的日期
        /// </summary>
        /// <returns></returns>
        public DateTime GetLunarNewYearDate()
        {
            ushort iYear, iMonth, iDay;

            TimeSpan ts;
            iYear = (ushort)(m_Date.Year);

            if ((iYear < START_YEAR) || (iYear > END_YEAR)) { return DateTime.MinValue; };

            ts = m_Date - (new DateTime(START_YEAR, 1, 1));
            l_CalcLunarDate(out iYear, out iMonth, out iDay, (uint)(ts.Days));

            // 今年過年至今的農曆天數
            uint days = 0;

            for (ushort i = 1; i < iMonth; i++)
            {
                days += LunarMonthDays(iYear, i);
            }

            days += iDay - (uint)1;

            return m_Date.AddDays(-days);
        }

        /// <summary>
        /// 取得明年的農曆過年的日期
        /// </summary>
        /// <returns></returns>
        public DateTime GetNextLunarNewYearDate()
        {
            ushort iYear, iMonth, iDay;

            TimeSpan ts;
            iYear = (ushort)(m_Date.AddYears(1).Year) ; //明年

            if ((iYear < START_YEAR) || (iYear > END_YEAR)) { return DateTime.MinValue; };

            ts = m_Date.AddYears(1) - (new DateTime(START_YEAR, 1, 1));
            l_CalcLunarDate(out iYear, out iMonth, out iDay, (uint)(ts.Days));

            // 今年過年至今的農曆天數
            uint days = 0;

            for (ushort i = 1; i < iMonth; i++)
            {
                days += LunarMonthDays(iYear, i);
            }

            days += iDay - (uint)1;

            return m_Date.AddYears(1).AddDays(-days);
        }

        /// <summary>
        /// 取得今年的農曆過年的日期
        /// </summary>
        /// <returns></returns>
        public DateTime GetLunarDate(ushort iMonth, ushort iDay)
        {
            ushort iYear;

            TimeSpan ts;
            iYear = (ushort)(m_Date.Year);

            if ((iYear < START_YEAR) || (iYear > END_YEAR)) { return DateTime.MinValue; };

            // 今年過年至今的農曆天數
            uint days = 0;

            for (ushort i = 1; i < iMonth; i++)
            {
                days += LunarMonthDays(iYear, i);
            }

            days += iDay - (uint)1;

            DateTime ThisNewYearDate = this.GetLunarNewYearDate();

            return ThisNewYearDate.AddDays(days);
        }

        /// <summary>
        /// 取得農曆的日期
        /// </summary>
        /// <returns></returns>
        public string GetLunarHolDay()
        {
            ushort iYear, iMonth, iDay;
            int i;
            TimeSpan ts;
            iYear = (ushort)(m_Date.Year);
            if ((iYear < START_YEAR) || (iYear > END_YEAR))
            { return ""; };
            i = l_GetLunarHolDay();
            if ((i >= 0) && (i <= 23))
            { return LunarHolDayName[i]; }
            else
            {
                ts = m_Date - (new DateTime(START_YEAR, 1, 1));
                l_CalcLunarDate(out iYear, out iMonth, out iDay, (uint)(ts.Days));
                return FormatMonth(iMonth, true) + FormatLunarDay(iDay);
            }
        }

        /// <summary>
        /// 返回農曆 iLunarYear 年(西元年) 的閏月月份,如沒有返回 0 (有效期間為:1901年1月---2050年12月)
        /// </summary>
        /// <param name="iLunarYear"></param>
        /// <returns></returns>
        public int GetLeapMonth(ushort iLunarYear)
        {
            byte Flag;
            if ((iLunarYear < START_YEAR) || (iLunarYear > END_YEAR))
            { return 0; };
            Flag = gLunarMonth[(iLunarYear - START_YEAR) / 2];
            if ((iLunarYear - START_YEAR) % 2 == 0)
            { return Flag >> 4; }
            else
            { return Flag & 0x0F; }
        }

        /// <summary>
        /// 取回農曆 iLunarYer 年與農曆 iLunarMonth 月的農曆天數
        /// 如果 iLunarMonth 為閏月,高字為第二個 iLunarMonth 月的天數,否則高字為0 1901年1月---2050年12月
        /// </summary>
        /// <param name="iLunarYear">農曆年份</param>
        /// <param name="iLunarMonth">農曆月份</param>
        /// <returns></returns>
        public uint LunarMonthDays(ushort iLunarYear, ushort iLunarMonth)
        {
            int Height, Low;
            int iBit;
            if ((iLunarYear < START_YEAR) || (iLunarYear > END_YEAR))
            {
                return 30;
            }
            Height = 0;
            Low = 29;
            iBit = 16 - iLunarMonth;
            if ((iLunarMonth > GetLeapMonth(iLunarYear)) && (GetLeapMonth(iLunarYear) > 0))
            {
                iBit--;
            }
            if ((gLunarMonthDay[iLunarYear - START_YEAR] & (1 << iBit)) > 0)
            {
                Low++;
            }
            if (iLunarMonth == GetLeapMonth(iLunarYear))
            {
                if ((gLunarMonthDay[iLunarYear - START_YEAR] & (1 << (iBit - 1))) > 0)
                { Height = 30; }
                else
                { Height = 29; }
            }
            return (uint)((uint)(Low) | (uint)(Height) << 16); //合成為uint
        }

        /// <summary>
        /// 取得農曆 iLunarYear 年的總天數
        /// </summary>
        /// <param name="iLunarYear">農曆年</param>
        /// <returns></returns>
        public int LunarYearDays(ushort iLunarYear)
        {
            int Days;
            uint tmp;
            if ((iLunarYear < START_YEAR) || (iLunarYear > END_YEAR))
            { return 0; };
            Days = 0;
            for (ushort i = 1; i <= 12; i++)
            {
                tmp = LunarMonthDays(iLunarYear, i);
                Days = Days + ((ushort)(tmp >> 16) & 0xFFFF); //取高位
                Days = Days + (ushort)(tmp); //取低位
            }
            return Days;
        }

        /// <summary>
        /// 計算從1901年1月1日過 iSpanDays 天後的農曆日期
        /// </summary>
        /// <param name="iYear">傳出農曆的年份</param>
        /// <param name="iMonth">傳出農曆的月份</param>
        /// <param name="iDay">傳出農曆的日</param>
        /// <param name="iSpanDays">傳入從 1901/1/1 之後的天數</param>
        public void l_CalcLunarDate(out ushort iYear, out ushort iMonth, out ushort iDay, uint iSpanDays)
        {
            uint tmp;
            //陽曆1901年2月19日為農曆1901年正月初一
            //陽曆1901年1月1日到2月19日共有49天
            if (iSpanDays < 49)
            {
                iYear = START_YEAR - 1;
                if (iSpanDays < 19)
                {
                    iMonth = 11;
                    iDay = (ushort)(11 + iSpanDays);
                }
                else
                {
                    iMonth = 12;
                    iDay = (ushort)(iSpanDays - 18);
                }
                return;
            }
            //下面從農曆1901年正月初一算起
            iSpanDays = iSpanDays - 49;
            iYear = START_YEAR;
            iMonth = 1;
            iDay = 1;
            //計算年
            tmp = (uint)LunarYearDays(iYear);
            while (iSpanDays >= tmp)
            {
                iSpanDays = iSpanDays - tmp;
                iYear++;
                tmp = (uint)LunarYearDays(iYear);
            }
            //計算月
            tmp = LunarMonthDays(iYear, iMonth); //取低位
            while (iSpanDays >= tmp)
            {
                iSpanDays = iSpanDays - tmp;
                if (iMonth == GetLeapMonth(iYear))
                {
                    tmp = (LunarMonthDays(iYear, iMonth) >> 16) & 0xFFFF; //取高位
                    if (iSpanDays < tmp)
                    { break; }
                    iSpanDays = iSpanDays - tmp;
                }
                iMonth++;
                tmp = LunarMonthDays(iYear, iMonth); //取低位
            }
            //計算日
            iDay = (ushort)(iDay + iSpanDays);
        }


        /// <summary>
        /// 把 iYear 年格式化成「天干記年法」表示的字串符號
        /// </summary>
        /// <returns></returns>
        public string FormatLunarYear()
        {
            string strYear;
            string szText1 = "甲乙丙丁戊己庚辛壬癸";
            string szText2 = "子丑寅卯辰巳午未申酉戌亥";
            string szText3 = "鼠牛虎免龍蛇馬羊猴雞狗豬";
            ushort iYear;
            iYear = (ushort)(m_Date.Year);
            strYear = szText1.Substring((iYear - 4) % 10, 1);
            strYear = strYear + szText2.Substring((iYear - 4) % 12, 1);
            strYear = strYear + " ";
            strYear = strYear + szText3.Substring((iYear - 4) % 12, 1);
            strYear = strYear + "年";
            return strYear;
        }
    }

    // 一些使用的範例程式

    //for (int i = 2007; i <= 2050; i++)
    //{
    //    CNCalendar.CNDate cd1 = new CNCalendar.CNDate(new DateTime(i, DateTime.Now.Month, DateTime.Now.Day));
    //    Response.Write("計算 " + i + "年隔年農曆過年的國曆日期:" + cd1.GetNextLunarNewYearDate().ToLongDateString() + "<br/>");
    //}

    //CNCalendar.CNDate cd = new CNCalendar.CNDate();

    //Response.Write("今年農曆天數:" + cd.LunarYearDays(2007).ToString() + "<br/>");
    //Response.Write("今年 01 月農曆天數:" + cd.LunarMonthDays(2007, 1).ToString() + "<br/>");
    //Response.Write("今年 02 月農曆天數:" + cd.LunarMonthDays(2007, 2).ToString() + "<br/>");
    //Response.Write("今年 03 月農曆天數:" + cd.LunarMonthDays(2007, 3).ToString() + "<br/>");
    //Response.Write("今年 04 月農曆天數:" + cd.LunarMonthDays(2007, 4).ToString() + "<br/>");
    //Response.Write("今年 05 月農曆天數:" + cd.LunarMonthDays(2007, 5).ToString() + "<br/>");
    //Response.Write("今年 06 月農曆天數:" + cd.LunarMonthDays(2007, 6).ToString() + "<br/>");
    //Response.Write("今年 07 月農曆天數:" + cd.LunarMonthDays(2007, 7).ToString() + "<br/>");
    //Response.Write("今年 08 月農曆天數:" + cd.LunarMonthDays(2007, 8).ToString() + "<br/>");
    //Response.Write("今年 09 月農曆天數:" + cd.LunarMonthDays(2007, 9).ToString() + "<br/>");
    //Response.Write("今年 10 月農曆天數:" + cd.LunarMonthDays(2007, 10).ToString() + "<br/>");
    //Response.Write("今年 11 月農曆天數:" + cd.LunarMonthDays(2007, 11).ToString() + "<br/>");
    //Response.Write("今年 12 月農曆天數:" + cd.LunarMonthDays(2007, 12).ToString() + "<br/>");

    //Response.Write("今天的農曆日期:" + cd.GetLunarHolDay() + "<br/>");
    //Response.Write("今年的農曆潤月月份:" + cd.GetLeapMonth(2007) + "<br/>");
    //Response.Write("計算國曆當天對應的節氣:" + cd.l_GetLunarHolDay() + "<br/>");

    //Response.Write("計算今年農曆過年的國曆日期:" + cd.GetLunarNewYearDate().ToLongDateString() + "<br/>");
}

 

留言評論