zoukankan      html  css  js  c++  java
  • C#获取农历的日期(转)

    //C# 获取农历日期
    
    ///<summary>
    /// 实例化一个 ChineseLunisolarCalendar
    ///</summary>
    private static  ChineseLunisolarCalendar ChineseCalendar =new ChineseLunisolarCalendar();
     
    ///<summary>
    /// 十天干
    ///</summary>
    private static string[] tg = { "", "", "", "", "", "", "", "", "", "" };
     
    ///<summary>
    /// 十二地支
    ///</summary>
    private static string[] dz = { "", "", "", "", "", "", "", "", "", "", "", "" };
     
    ///<summary>
    /// 十二生肖
    ///</summary>
    private static string[] sx = { "", "", "", "", "", "", "", "", "", "", "", "" };
     
    ///<summary>
    /// 返回农历天干地支年
    ///</summary>
    ///<param name="year">农历年</param>
    ///<return s></return s>
    public static string GetLunisolarYear(int year)
    {
        if (year >3)
        {
            int tgIndex = (year -4) %10;
            int dzIndex = (year -4) %12;
             
            return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
        }
         
        throw new ArgumentOutOfRangeException("无效的年份!");
    }
     
    ///<summary>
    /// 农历月
    ///</summary>
     
    ///<return s></return s>
    private static string[] months = { "", "", "", "", "", "", "", "", "", "", "十一", "十二(腊)" };
     
    ///<summary>
    /// 农历日
    ///</summary>
    private static string[] days1 = { "", "", "廿", "" };
    ///<summary>
    /// 农历日
    ///</summary>
    private static string[] days = { "", "", "", "", "", "", "", "", "", "" };
     
     
    ///<summary>
    /// 返回农历月
    ///</summary>
    ///<param name="month">月份</param>
    ///<return s></return s>
    public static string GetLunisolarMonth(int month)
    {
        if (month <13 && month >0)
        {
            return  months[month -1];
        }
         
        throw  new ArgumentOutOfRangeException("无效的月份!");
    }
     
    ///<summary>
    /// 返回农历日
    ///</summary>
    ///<param name="day"></param>
    ///<return s></return s>
    public static string GetLunisolarDay(int day)
    {
        if (day >0 && day <32)
        {
            if (day !=20 && day !=30)
            {
                return string.Concat(days1[(day -1) /10], days[(day -1) %10]);
            }
            else
            {
                return string.Concat(days[(day -1) /10], days1[1]);
             }
        }
         
        throw new ArgumentOutOfRangeException("无效的日!");
    }
     
     
     
    ///<summary>
    /// 根据公历获取农历日期
    ///</summary>
    ///<param name="datetime">公历日期</param>
    ///<return s></return s>
    public static string GetChineseDateTime(DateTime datetime)
    {
        int year = ChineseCalendar.GetYear(datetime);
        int month = ChineseCalendar.GetMonth(datetime);
        int day = ChineseCalendar.GetDayOfMonth(datetime);
        //获取闰月, 0 则表示没有闰月
        int leapMonth = ChineseCalendar.GetLeapMonth(year);
         
        bool isleap =false;
         
        if (leapMonth >0)
        {
            if (leapMonth == month)
            {
            //闰月
                isleap =true;
                month--;
            }
            else if (month > leapMonth)
            {
                 month--;
            }
        }
         
        return string.Concat(GetLunisolarYear(year), "", isleap ?"" : string.Empty, GetLunisolarMonth(month), "", GetLunisolarDay(day));
    }

    使用方法:把这段代码写进去后,在要使用的地方引用:

     string lunar = GetChineseDateTime(DateTime.Now);
  • 相关阅读:
    【Vegas原创】mysql更改用户密码之无敌方法
    【Vegas原创】Xcopy屡试不爽
    【Vegas原创】ctrl shift无法切换输入法的解决方法
    【Vegas原创】将SQLServer表、视图、存储过程的所有者批量改为dbo的处理方法
    【Vegas原创】SQL Server2005应急备机切换步骤 生产机正常
    【Vegas原创】SQLServer 2000 企业管理器展开数据库列表错误的解决方法
    【Vegas原创】win7下打开telnet服务
    【Vegas原创】Windows 2003下CACTI的安装及配置
    【Vegas原创】SecureCRT个性化设置
    Mathematica实现微分算子功能
  • 原文地址:https://www.cnblogs.com/jackcheblog/p/7209947.html
Copyright © 2011-2022 走看看