http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 23806|回复: 2

[功能帮助类] C#农历帮助类,带有节日和节气

[复制链接]
发表于 2015-12-26 11:48:12 | 显示全部楼层 |阅读模式
网上已经有很多C#的农历类,但看来都很复杂,其实C#中已经提供了中国的农历类ChineseLunisolarCalendar。但未提供节气和节日功能。
根据网上的方法字节写了一个中国农历类!
ChinaDate.GetChinaDate(DateTime dt) 获取农历日期
ChinaDate.GetYear(DateTime dt) 获取农历年份(包含天干地支和生肖信息)
ChinaDate.GetMonth(DateTime dt) 获取农历月份
ChinaDate.GetDay(DateTime dt) 获取农历日期
ChinaDate.GetSolarTerm(DateTime dt) 获取节气
ChinaDate.GetHoliday(DateTime dt) 获取公历节日
ChinaDate.GetChinaHoliday(DateTime dt) 获取农历节日
[C#] 纯文本查看 复制代码
using System.Globalization;
using System.Collections;
using System;
 
/// <summary>
/// 中国农历
/// </summary>
/// 作者:[url]http://www.sufeinet.com[/url]
public static class ChinaDate
{
    private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar();
    private static Hashtable gHoliday = new Hashtable();
    private static Hashtable nHoliday = new Hashtable();
    private static string[] JQ = { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
    private static int[] JQData = { 0, 21208, 43467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
 
    static ChinaDate()
    {
        //公历节日
        gHoliday.Add("0101", "元旦");
        gHoliday.Add("0214", "情人节");
        gHoliday.Add("0305", "雷锋日");
        gHoliday.Add("0308", "妇女节");
        gHoliday.Add("0312", "植树节");
        gHoliday.Add("0315", "消费者权益日");
        gHoliday.Add("0401", "愚人节");
        gHoliday.Add("0501", "劳动节");
        gHoliday.Add("0504", "青年节");
        gHoliday.Add("0601", "儿童节");
        gHoliday.Add("0701", "建党节");
        gHoliday.Add("0801", "建军节");
        gHoliday.Add("0910", "教师节");
        gHoliday.Add("1001", "国庆节");
        gHoliday.Add("1224", "平安夜");
        gHoliday.Add("1225", "圣诞节");
 
        //农历节日
        nHoliday.Add("0101", "春节");
        nHoliday.Add("0115", "元宵节");
        nHoliday.Add("0505", "端午节");
        nHoliday.Add("0815", "中秋节");
        nHoliday.Add("0909", "重阳节");
        nHoliday.Add("1208", "腊八节");
    }
 
    /// <summary>
    /// 获取农历
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string GetChinaDate(DateTime dt)
    {
        if (dt > china.MaxSupportedDateTime || dt < china.MinSupportedDateTime)
        {
            //日期范围:1901 年 2 月 19 日 - 2101 年 1 月 28 日
            throw new Exception(string.Format("日期超出范围!必须在{0}到{1}之间!", china.MinSupportedDateTime.ToString("yyyy-MM-dd"), china.MaxSupportedDateTime.ToString("yyyy-MM-dd")));
        }
        string str = string.Format("{0} {1}{2}", GetYear(dt), GetMonth(dt), GetDay(dt));
        string strJQ = GetSolarTerm(dt);
        if (strJQ != "")
        {
            str += " (" + strJQ + ")";
        }
        string strHoliday = GetHoliday(dt);
        if (strHoliday != "")
        {
            str += " " + strHoliday;
        }
        string strChinaHoliday = GetChinaHoliday(dt);
        if (strChinaHoliday != "")
        {
            str += " " + strChinaHoliday;
        }
 
        return str;
    }
 
    /// <summary>
    /// 获取农历年份
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string GetYear(DateTime dt)
    {
        int yearIndex = china.GetSexagenaryYear(dt);
        string yearTG = " 甲乙丙丁戊己庚辛壬癸";
        string yearDZ = " 子丑寅卯辰巳午未申酉戌亥";
        string yearSX = " 鼠牛虎兔龙蛇马羊猴鸡狗猪";
        int year = china.GetYear(dt);
        int yTG = china.GetCelestialStem(yearIndex);
        int yDZ = china.GetTerrestrialBranch(yearIndex);
 
        string str = string.Format("[{1}]{2}{3}{0}", year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]);
        return str;
    }
 
    /// <summary>
    /// 获取农历月份
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string GetMonth(DateTime dt)
    {
        int year = china.GetYear(dt);
        int iMonth = china.GetMonth(dt);
        int leapMonth = china.GetLeapMonth(year);
        bool isLeapMonth = iMonth == leapMonth;
        if (leapMonth != 0 && iMonth >= leapMonth)
        {
            iMonth--;
        }
 
        string szText = "正二三四五六七八九十";
        string strMonth = isLeapMonth ? "闰" : "";
        if (iMonth <= 10)
        {
            strMonth += szText.Substring(iMonth - 1, 1);
        }
        else if (iMonth == 11)
        {
            strMonth += "十一";
        }
        else
        {
            strMonth += "腊";
        }
        return strMonth + "月";
    }
 
    /// <summary>
    /// 获取农历日期
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string GetDay(DateTime dt)
    {
        int iDay = china.GetDayOfMonth(dt);
        string szText1 = "初十廿三";
        string szText2 = "一二三四五六七八九十";
        string strDay;
        if (iDay == 20)
        {
            strDay = "二十";
        }
        else if (iDay == 30)
        {
            strDay = "三十";
        }
        else
        {
            strDay = szText1.Substring((iDay - 1) / 10, 1);
            strDay = strDay + szText2.Substring((iDay - 1) % 10, 1);
        }
        return strDay;
    }
 
    /// <summary>
    /// 获取节气
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string GetSolarTerm(DateTime dt)
    {
        DateTime dtBase = new DateTime(1900, 1, 6, 2, 5, 0);
        DateTime dtNew;
        double num;
        int y;
        string strReturn = "";
 
        y = dt.Year;
        for (int i = 1; i <= 24; i++)
        {
            num = 525948.76 * (y - 1900) + JQData[i - 1];
            dtNew = dtBase.AddMinutes(num);
            if (dtNew.DayOfYear == dt.DayOfYear)
            {
                strReturn = JQ[i - 1];
            }
        }
 
        return strReturn;
    }
 
    /// <summary>
    /// 获取公历节日
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string GetHoliday(DateTime dt)
    {
        string strReturn = "";
        object g = gHoliday[dt.Month.ToString("00") + dt.Day.ToString("00")];
        if (g != null)
        {
            strReturn = g.ToString();
        }
 
        return strReturn;
    }
 
    /// <summary>
    /// 获取农历节日
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string GetChinaHoliday(DateTime dt)
    {
        string strReturn = "";
        int year = china.GetYear(dt);
        int iMonth = china.GetMonth(dt);
        int leapMonth = china.GetLeapMonth(year);
        int iDay = china.GetDayOfMonth(dt);
        if (china.GetDayOfYear(dt) == china.GetDaysInYear(year))
        {
            strReturn = "除夕";
        }
        else if (leapMonth != iMonth)
        {
            if (leapMonth != 0 && iMonth >= leapMonth)
            {
                iMonth--;
            }
            object n = nHoliday[iMonth.ToString("00") + iDay.ToString("00")];
            if (n != null)
            {
                if (strReturn == "")
                {
                    strReturn = n.ToString();
                }
                else
                {
                    strReturn += " " + n.ToString();
                }
            }
        }
 
        return strReturn;
    }
}
也可以直接下载源码
ChinaDate.zip (2.07 KB, 下载次数: 361)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2020-1-27 16:20:49 | 显示全部楼层
站长,这个农历帮助类,节气的算法有误差,比如2020年立春为2月4日,但是你这个算法,立春为2月5日,麻烦站长大人解决下!
发表于 2020-1-27 16:41:23 | 显示全部楼层
你好 站长
C#农历帮助类,带有节日和节气 , 这个算法存在误差。
比如2020年立春是2月4日,但是你提供的这个算法为2月5日!我技术有限,不知道如何修改?
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-22 01:29

© 2014-2021

快速回复 返回顶部 返回列表