KingAmu'Code

NET显示今天的农历日期

引用命名空间:using System.Globalization;
调用需要实例化 :

        CNDate cdate = new CNDate();
        Response.Write(cdate.GetCNDate());

 

///<summary>
/// 显示今天农历类
///</summary>
public class CNDate
{
    private DateTime m_Date; //今天的日期
    private int cny; //农历的年对应甲子循环中的年
    private int cnm; //农历的月-1
    private int cnd; //农历日
    private int icnm; //农历闰月
    ///<summary>
    /// 显示日期构造函数
    ///</summary>
    public CNDate()
    {
    }

    ///<summary>
    /// 返回格式化的农历显示
    ///</summary>
    ///<returns>格式如:戊子(鼠)年润四月廿三</returns>
    public string GetCNDate()
    {
        m_Date = DateTime.Today;    //DateTime.Today;  //2010-2-22
        ChineseLunisolarCalendar cnCalendar = new ChineseLunisolarCalendar();
        cny = cnCalendar.GetSexagenaryYear(m_Date); //27  计算与指定日期对应的甲子(60 年)循环中的年。 
        cnm = cnCalendar.GetMonth(m_Date); //1  返回指定日期中的月份-1
        cnd = cnCalendar.GetDayOfMonth(m_Date); //9 计算指定日期中的月中日期。 
        icnm = cnCalendar.GetLeapMonth(cnCalendar.GetYear(m_Date)); //0 计算指定纪元年份的闰月。
        string txcns = "";
        const string szText1 = "癸甲乙丙丁戊己庚辛壬";
        const string szText2 = "亥子丑寅卯辰巳午未申酉戌";
        const string szText3 = "猪鼠牛虎免龙蛇马羊猴鸡狗";
        int tn = cny % 10; //天干
        int dn = cny % 12;  //地支
        txcns += szText1.Substring(tn, 1);
        txcns += szText2.Substring(dn, 1);
        txcns += "(" + szText3.Substring(dn, 1) + ")年"; //格式化月份显示
        string[] cnMonth = { "", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月", "十二月" };
        string sMonth = "";

        //润月的处理
        if (icnm > 0 && cnm <= icnm)
        {
            cnMonth[icnm] = "闰" + cnMonth[icnm - 1];
            sMonth = cnMonth[cnCalendar.GetMonth(m_Date)];
        }
        else if (icnm > 0 && cnm > icnm)
        {
            sMonth = cnMonth[cnCalendar.GetMonth(m_Date) - 1];
        }
        else
        {
            sMonth = cnMonth[cnCalendar.GetMonth(m_Date)];
        }
        txcns += sMonth;
        string[] cnDay ={ "", "初一", "初二", "初三", "初四", "初五", "初六", "初七"
                , "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六"
                , "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五"
                , "廿六", "廿七", "廿八", "廿九", "三十" };
        txcns += cnDay[cnd];
        return txcns;
    }
}

posted on 2010-03-21 12:50  阿牧  阅读(5208)  评论(0编辑  收藏  举报

导航