C#下的一個好用的日曆庫(sxtwl_cpp),支持農曆轉公曆,和公曆轉農曆等功能

sxtwl_cpp是壽星天文歷的C++版本實現。支持多種語言的綁定git

代碼首頁github

懶人包微信

懶人包使用方法

一、右鏈工程中的引用-》添加引用-》瀏覽-》選中dotnet目錄下的sxtwl.net.dllspa

二、生成解決方案-》找生成解決方案有exe目錄下,將bin目錄下dll拷貝過去(建議使用win32的dll, x64的DLL可能跑不起來).net

功能代碼演示:code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sxtwl_csharp
{
    class Program
    {
        string[] Gan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
        string[] Zhi = { "子", "醜", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
        string[] ShX = { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };
        string[] numCn = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" }; //中文數字
        string[] jqmc = { "冬至", "小寒", "大寒", "立春", "雨水", "驚蟄", "春分", "清明", "穀雨", "立夏", "小滿", "芒種", "夏至", "小暑", "大暑", "立秋", "處暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪" };
        string[] ymc = { "十一", "十二", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十" }; //月名稱,建寅
        string[] rmc = { "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十", "卅一" };

        private void log(String str)
        {
            System.Console.WriteLine(str);
        }


        private void printDay(Day day)
        {
            log("===================================================");
            log("公曆:" + day.y + "年" + (int)day.m + "月" + day.d + "日");

            if (day.Lleap)//是不是陰曆潤月
            {
                log("農曆:" +  "潤"  + (day.Lyear0 + 1984) + "年" + ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日");
            }
            else
            {
                log("農曆:" + "潤" + (day.Lyear0 + 1984) + "年" +   ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日");
            }

            log("儒略曆:JD" + sxtwl.J2000 + day.d0);
            log("星期" + numCn[day.week]);

            log("天干:" + Gan[day.Lyear2.tg] + Zhi[day.Lyear2.dz] + "年"
                    + Gan[day.Lmonth2.tg] + Zhi[day.Lmonth2.dz] + "月"
                    + Gan[day.Lday2.tg] + Zhi[day.Lday2.dz] + "日"
            );

            if (day.qk >= 0)
            {
                log("當日節氣:" + jqmc[day.jqmc]);
                log("節氣時間:" + day.jqsj);
            }

            //day.getXiZ();   //這個是星座

            log("距冬至" + day.cur_dz + "天");
            log("距夏至" + day.cur_xz + "天");
            log("距立秋" + day.cur_lq + "天");
            log("距芒種" + day.cur_mz + "天");
            log("距小暑" + day.cur_xs + "天");
        }


        private void testLunar()
        {
            Lunar lunar = new Lunar();

            //查詢某月的月份信息(如:查2017年10月份)
            Month month = lunar.yueLiCalc(2017, 10);
            //年生肖
            log(month.y + "年:" + ShX[month.ShX]);
            //年天干地支
            log("天干:" + Gan[month.yearGan] + Zhi[month.yearZhi]);
            //當月的全部的日信息

            for (int i = 0; i < month.days.Count; ++i)
            {
                Day tmpDay = month.days[i];
                printDay(tmpDay);
            }


            //獲取某個陽曆日的信息(可在信息中查詢到陰曆,天干等)
            Day day = lunar.getDayBySolar(2018, 10, 1);
            printDay(day);

            //獲取某個陰曆日的信息(可在信息中查詢到陽曆,天干等)
            Day day2 = lunar.getDayByLunar(2018, 10, 1);
            printDay(day2);

            //獲取時辰上的那個天干(例如拿下午13點的時辰干支)
            GZ gz = lunar.getShiGz(day.Lday2.tg, 13);
            log("獲取時辰的干支:" + Gan[gz.tg] + Zhi[gz.dz]);

            //獲取一年的信息(干支,生肖,)
            Year year = lunar.getYearCal(2018);
            log("獲取年的干支:" + Gan[year.yearGan] + Zhi[year.yearZhi]);
            log("獲取年的生肖:" + ShX[year.ShX]);
        }

        static void Main(string[] args)
        {

            new Program().testLunar();
            System.Console.ReadKey();
        }
    }
}

微信交流羣

若失效,請郵件至:lifulinghan@aol.com 索要新的二維碼blog

相關文章
相關標籤/搜索