农历日期封装类

在我们做管理系统或者要做网址导航都会涉及到日期的操作,下面写了一个这样的类供大家参考,项目中可以直接使用。项目中引用System.Globalization的命名空间,使用的是

ChineseLunisolarCalendar 类是针对是针对中国的日历类,里面有计算闰月和农历日期的函数,废话不说了,贴代码。

效果仿照hao123网址导航如图:

public class ChineseCalendar 

{

    private int chineseYear;

    private int chineseMonth;

    private int chineseDay;



    public  ChineseCalendar() 

    {



        ChineseLunisolarCalendar CLCalendar = new ChineseLunisolarCalendar();

        DateTime nowDt = System.DateTime.Now;



        chineseYear = CLCalendar.GetYear(nowDt);

        chineseMonth = CLCalendar.GetMonth(nowDt);

        chineseDay = CLCalendar.GetDayOfMonth(nowDt);





        int LeapMonth = CLCalendar.GetLeapMonth(chineseYear);       //GetLeapMonth计算指定年份的闰月(1-13或者0)



        //如果闰8月返回9 如果闰9月返回10

        if (LeapMonth != 0)

        {

            if (LeapMonth <= chineseMonth)

            {

                chineseMonth--;

            }

           

        }

       



    }



    /// <summary>

    /// 农历年

    /// </summary>

    public string ChineseYear 

    {

        

        get {

            return chineseYear.ToString();

        }

      

    }

    /// <summary>

    /// 农历月

    /// </summary>

    public string ChineseMonth 

    {

        get {







           string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一","腊" };



            return months[chineseMonth-1];

        }

    }

    /// <summary>

    /// 农历日

    /// </summary>

    public string ChineseDay 

    {

        get {

            

            string[] FirstDay = { "初", "十", "廿", "三" };

            string[] SecondDay = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};



            if(chineseDay!=20 && chineseDay!=30)

            {

                return string.Concat(FirstDay[(chineseDay - 1) / 10], SecondDay[(chineseDay - 1) % 10]);

            }

            else

            {

              return string.Concat(SecondDay[chineseDay-1],FirstDay[1]);

            }

        }

    }

    /// <summary>

    /// 中文日期格式为 :11月12号 

    /// </summary>

    public string ChineseDate 

    {

        get {

            return string.Format("{0}月{1}日",DateTime.Now.Month,DateTime.Now.Day);

        }

    }

    /// <summary>

    /// 中文星期格式为:星期一

    /// </summary>

    public string ChineseWeek 

    {

        get {

            return System.DateTime.Today.ToString("dddd", new System.Globalization.CultureInfo("zh-CN"));

        }

    }

    /// <summary>

    /// 重载Tostring指定格式为如:农历九月廿九

    /// </summary>

    /// <returns></returns>

    public override string ToString()

    {

        return string.Format("农历{0}月{1}",ChineseMonth,ChineseDay);

    }

   

}

  调用就比较方便了直接调用类属性就可以了!如LBdate.Text = ccl.ChineseDate + " " + ccl.ChineseWeek + "(" +ccl.ToString() + ")";

你可能感兴趣的:(日期)