Exemple #1
1
        public string cDate()
        {
            ChineseLunisolarCalendar l = new ChineseLunisolarCalendar();
            DateTime dt = DateTime.Today; //转换当日的日期
            //dt = new DateTime(2006, 1,29);//农历2006年大年初一(测试用),也可指定日期转换
            string[] aMonth = { "", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "腊月", "腊月" };
            //a10表示日期的十位!
            string[] a10 = { "初", "十", "廿", "卅" };
            string[] aDigi = { "O", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            string sYear = "", sYearArab = "", sMonth = "", sDay = "", sDay10 = "", sDay1 = "", sLuniSolarDate = "";
            int iYear, iMonth, iDay;
            iYear = l.GetYear(dt);
            iMonth = l.GetMonth(dt);
            iDay = l.GetDayOfMonth(dt);

            //Format Year
            sYearArab = iYear.ToString();
            for (int i = 0; i < sYearArab.Length; i++)
            {
                sYear += aDigi[Convert.ToInt16(sYearArab.Substring(i, 1))];
            }

            //Format Month
            int iLeapMonth = l.GetLeapMonth(iYear); //获取闰月

            /**/
            /*闰月可以出现在一年的任何月份之后。例如,GetMonth 方法返回一个介于 1 到 13 之间的数字来表示与指定日期关联的月份。如果在一年的八月和九月之间有一个闰月,则 GetMonth 方法为八月返回 8,为闰八月返回 9,为九月返回 10。*/

            if (iLeapMonth > 0 && iMonth <= iLeapMonth)
            {
                //for (int i = iLeapMonth + 1; i < 13; i++) aMonth[i] = aMonth[i - 1];
                aMonth[iLeapMonth] = "闰" + aMonth[iLeapMonth - 1];
                sMonth = aMonth[l.GetMonth(dt)];
            }
            else if (iLeapMonth > 0 && iMonth > iLeapMonth)
            {
                sMonth = aMonth[l.GetMonth(dt) - 1];
            }
            else
            {
                sMonth = aMonth[l.GetMonth(dt)];
            }

            //Format Day
            sDay10 = a10[iDay / 10];
            sDay1 = aDigi[(iDay % 10)];
            sDay = sDay10 + sDay1;

            if (iDay == 10) sDay = "初十";
            if (iDay == 20) sDay = "二十";
            if (iDay == 30) sDay = "三十";

            //Format Lunar Date
            //sLuniSolarDate = dt.Year+"年"+dt.Month+"月"+dt.Day+"日 "+Weeks(dt.DayOfWeek.ToString())+" 农历" + sYear + "年" + sMonth + sDay;
            sLuniSolarDate = dt.Year + "年" + dt.Month + "月" + dt.Day + "日 " + Weeks(dt.DayOfWeek.ToString()) + " 农历" + sMonth + sDay;
            return sLuniSolarDate;
        }
    /// <summary>
    /// 公历转为农历的函数
    /// </summary>
    /// <remarks>作者:DeltaCat</remarks>
    /// <example>网址:http://www.zu14.cn</example>
    /// <param name="solarDateTime">公历日期</param>
    /// <returns>农历的日期</returns>
    static string SolarToChineseLunisolarDate(DateTime solarDateTime)
    {
        System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();

        int    year         = cal.GetYear(solarDateTime);
        int    month        = cal.GetMonth(solarDateTime);
        int    day          = cal.GetDayOfMonth(solarDateTime);
        int    leapMonth    = cal.GetLeapMonth(year);
        string stringFormat = $"农历" +
                              $"{"甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10]}" +
                              $"{"子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12]}" +
                              $"({"鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12]})年" +
                              $"{(month == leapMonth ? "闰" : "")}{"无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month]}月" +
        /// <summary>
        /// 返回指定公历日期对应的农历月
        /// </summary>
        /// <param name="calendar"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetLunisolarMonth(this ChineseLunisolarCalendar calendar, DateTime date)
        {
            var month     = calendar.GetMonth(date);
            var leapMonth = calendar.GetLeapMonth(calendar.GetYear(date), calendar.GetEra(date));
            var isleep    = leapMonth > 0 && leapMonth == month;

            if (leapMonth > 0)
            {
                if (leapMonth <= month)
                {
                    month--;
                }
            }

            return(string.Concat(isleep ? "闰" : string.Empty, GetLunisolarMonth(month), "月"));
        }
        ///<summary>
        ///中国式的时间格式
        ///子时:23:00-1:00
        ///丑时:1:00-3:00
        ///寅时:3:00-5:00
        ///卯时:5:00-7:00
        ///辰时:7:00-9:00
        ///巳时:9:00-11:00
        ///午时:11:00-13:00
        ///未时:13:00-15:00
        ///申时:15:00-17:00
        ///酉时:17:00-19:00
        ///戌时:19:00-21:00
        ///亥时:21:00-23:00
        /// </summary>
        /// <param name="date">时间参数</param>
        /// <returns></returns>
        public static string GetChineseDate(DateTime date)
        {
            var cnDate = new ChineseLunisolarCalendar();
            string[] arrMonth = { "", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "冬月", "腊月" };
            string[] arrDay = { "", "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十" };
            string[] arrYear = { "", "甲子", "乙丑", "丙寅", "丁卯", "戊辰", "己巳", "庚午", "辛未", "壬申", "癸酉", "甲戌", "乙亥", "丙子", "丁丑", "戊寅", "己卯", "庚辰", "辛己", "壬午", "癸未", "甲申", "乙酉", "丙戌", "丁亥", "戊子", "己丑", "庚寅", "辛卯", "壬辰", "癸巳", "甲午", "乙未", "丙申", "丁酉", "戊戌", "己亥", "庚子", "辛丑", "壬寅", "癸丑", "甲辰", "乙巳", "丙午", "丁未", "戊申", "己酉", "庚戌", "辛亥", "壬子", "癸丑", "甲寅", "乙卯", "丙辰", "丁巳", "戊午", "己未", "庚申", "辛酉", "壬戌", "癸亥" };

            var lYear = cnDate.GetYear(date);
            var sYear = arrYear[cnDate.GetSexagenaryYear(date)];
            var lMonth = cnDate.GetMonth(date);
            var lDay = cnDate.GetDayOfMonth(date);

            //获取第几个月是闰月,等于0表示本年无闰月
            var leapMonth = cnDate.GetLeapMonth(lYear);
            var sMonth = arrMonth[lMonth];
            //如果今年有闰月
            if (leapMonth > 0)
            {
                //闰月数等于当前月份
                sMonth = lMonth == leapMonth ? string.Format("闰{0}", arrMonth[lMonth - 1]) : sMonth;
                sMonth = lMonth > leapMonth ? arrMonth[lMonth - 1] : sMonth;
            }
            return string.Format("{0}年{1}{2}", sYear, sMonth, arrDay[lDay]);
        }
Exemple #5
0
        //显示农历日期
        string GetCNDate()
        {
             DateTime m_Date; //今天的日期
             int cny; //农历的年月日
             int cnm; //农历的年月日
             int cnd; //农历的年月日
             int icnm; //农历闰月

             m_Date = DateTime.Today;
            ChineseLunisolarCalendar cnCalendar =new ChineseLunisolarCalendar();
            cny = cnCalendar.GetSexagenaryYear(m_Date);
            cnm = cnCalendar.GetMonth(m_Date);
            cnd = cnCalendar.GetDayOfMonth(m_Date);
            icnm = cnCalendar.GetLeapMonth(cnCalendar.GetYear(m_Date));


            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 ={ "", "正月", "二月", "三月", "四月", "五月", "六月"
                , "七月", "八月", "九月", "十月", "十一月", "十二月", "十二月" };
            if (icnm > 0)
            {
                for (int i = icnm + 1; i < 13; i++)
                    cnMonth[icnm] = cnMonth[icnm - 1];
                cnMonth[icnm] = "闰" + cnMonth[icnm];
            }
            txcns += cnMonth[cnm];
            string[] cnDay ={ "", "初一", "初二", "初三", "初四", "初五", "初六", "初七"
                , "初八", "初九", "初十", "十一", "十二", "十三"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   , "十四", "十五", "十六"
                , "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五"
                , "廿六", "廿七", "廿八", "廿九", "三十" };
            txcns += cnDay[cnd];
            return txcns;
        }
Exemple #6
0
	public void GetLeapMonth ()
	{
		GregorianCalendar gc = new GregorianCalendar ();
		Assert.AreEqual (0, gc.GetLeapMonth (2007), "#1-1");
		Assert.AreEqual (0, gc.GetLeapMonth (2008), "#1-2");
		Assert.AreEqual (0, gc.GetLeapMonth (2100), "#1-3");
		Assert.AreEqual (0, gc.GetLeapMonth (2000), "#1-4");

		JulianCalendar jc = new JulianCalendar ();
		Assert.AreEqual (0, jc.GetLeapMonth (2007), "#2-1");
		Assert.AreEqual (0, jc.GetLeapMonth (2008), "#2-2");
		Assert.AreEqual (0, jc.GetLeapMonth (2100), "#2-3");
		Assert.AreEqual (0, jc.GetLeapMonth (2000), "#2-4");
		Assert.AreEqual (0, jc.GetLeapMonth (2009), "#2-5");
		Assert.AreEqual (0, jc.GetLeapMonth (2010), "#2-6");

		HebrewCalendar hc = new HebrewCalendar ();
		// 3rd, 6th, 8th, 11th 14th and 17th year in every 19 are leap.
		// 5339 % 19 = 0.
		Assert.AreEqual (0, hc.GetLeapMonth (5343), "#3-1");
		Assert.AreEqual (0, hc.GetLeapMonth (5344), "#3-2");
		Assert.AreEqual (7, hc.GetLeapMonth (5345), "#3-3");
		Assert.AreEqual (0, hc.GetLeapMonth (5346), "#3-4");
		Assert.AreEqual (7, hc.GetLeapMonth (5347), "#3-5");
		Assert.AreEqual (0, hc.GetLeapMonth (5348), "#3-6");
		Assert.AreEqual (0, hc.GetLeapMonth (5349), "#3-7");

		ThaiBuddhistCalendar tc = new ThaiBuddhistCalendar ();
		Assert.AreEqual (0, tc.GetLeapMonth (2520), "#4-1");
		Assert.AreEqual (0, tc.GetLeapMonth (2521), "#4-2");
		Assert.AreEqual (0, tc.GetLeapMonth (2522), "#4-3");
		Assert.AreEqual (0, tc.GetLeapMonth (2523), "#4-4");

		ChineseLunisolarCalendar cc = new ChineseLunisolarCalendar ();
		Assert.AreEqual (0, cc.GetLeapMonth (2000), "#5-1");
		Assert.AreEqual (5, cc.GetLeapMonth (2001), "#5-2");
		Assert.AreEqual (0, cc.GetLeapMonth (2002), "#5-3");
		Assert.AreEqual (0, cc.GetLeapMonth (2003), "#5-4");
		Assert.AreEqual (3, cc.GetLeapMonth (2004), "#5-5");
		Assert.AreEqual (0, cc.GetLeapMonth (2005), "#5-6");
		Assert.AreEqual (8, cc.GetLeapMonth (2006), "#5-7");
		Assert.AreEqual (0, cc.GetLeapMonth (2007), "#5-8");
		Assert.AreEqual (0, cc.GetLeapMonth (2008), "#5-9");
		Assert.AreEqual (6, cc.GetLeapMonth (2009), "#5-10");
		Assert.AreEqual (0, cc.GetLeapMonth (2010), "#5-11");
		Assert.AreEqual (0, cc.GetLeapMonth (2011), "#5-12");
		Assert.AreEqual (5, cc.GetLeapMonth (2012), "#5-13");
		Assert.AreEqual (0, cc.GetLeapMonth (2013), "#5-14");
		Assert.AreEqual (10, cc.GetLeapMonth (2014), "#5-15");
		Assert.AreEqual (0, cc.GetLeapMonth (2015), "#5-16");
		Assert.AreEqual (0, cc.GetLeapMonth (2016), "#5-17");
		Assert.AreEqual (7, cc.GetLeapMonth (2017), "#5-18");
		Assert.AreEqual (0, cc.GetLeapMonth (2018), "#5-19");
		Assert.AreEqual (0, cc.GetLeapMonth (2019), "#5-20");

		KoreanLunisolarCalendar kc = new KoreanLunisolarCalendar ();
		Assert.AreEqual (0, kc.GetLeapMonth (2000), "#6-1");
		Assert.AreEqual (5, kc.GetLeapMonth (2001), "#6-2");
		Assert.AreEqual (0, kc.GetLeapMonth (2002), "#6-3");
		Assert.AreEqual (0, kc.GetLeapMonth (2003), "#6-4");
		Assert.AreEqual (3, kc.GetLeapMonth (2004), "#6-5");
		Assert.AreEqual (0, kc.GetLeapMonth (2005), "#6-6");
		Assert.AreEqual (8, kc.GetLeapMonth (2006), "#6-7");
		Assert.AreEqual (0, kc.GetLeapMonth (2007), "#6-8");
		Assert.AreEqual (0, kc.GetLeapMonth (2008), "#6-9");
		Assert.AreEqual (6, kc.GetLeapMonth (2009), "#6-10");
		Assert.AreEqual (0, kc.GetLeapMonth (2010), "#6-11");
		Assert.AreEqual (0, kc.GetLeapMonth (2011), "#6-12");
		Assert.AreEqual (4, kc.GetLeapMonth (2012)); // off from cn by 1, "#6-13");
		Assert.AreEqual (0, kc.GetLeapMonth (2013), "#6-14");
		Assert.AreEqual (10, kc.GetLeapMonth (2014), "#6-15");
		Assert.AreEqual (0, kc.GetLeapMonth (2015), "#6-16");
		Assert.AreEqual (0, kc.GetLeapMonth (2016), "#6-17");
		Assert.AreEqual (6, kc.GetLeapMonth (2017)); // off from cn by 1, "#6-18");
		Assert.AreEqual (0, kc.GetLeapMonth (2018), "#6-19");
		Assert.AreEqual (0, kc.GetLeapMonth (2019), "#6-20");
	}
Exemple #7
0
        string GetCNDate()
        {
            DateTime m_Date; //���������
            int cny; //ũ����������
            int cnm; //ũ����������
            int cnd; //ũ����������
            int icnm; //ũ������

            m_Date = DateTime.Today;
            ChineseLunisolarCalendar cnCalendar = new ChineseLunisolarCalendar();
            cny = cnCalendar.GetSexagenaryYear(m_Date);
            cnm = cnCalendar.GetMonth(m_Date);
            cnd = cnCalendar.GetDayOfMonth(m_Date);
            icnm = cnCalendar.GetLeapMonth(cnCalendar.GetYear(m_Date));

            string txcns = "ũ��";
            const string szText1 = "����ұ����켺������";
            const string szText2 = "���ӳ���î������δ������";
            const string szText3 = "����ţ������������F��";
            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 ={ "", "����", "����", "����", "����", "����", "����"
                , "����", "����", "����", "ʮ��", "ʮһ��", "ʮ����", "ʮ����" };
            if (icnm > 0)
            {
                for (int i = icnm + 1; i < 13; i++)
                    cnMonth[icnm] = cnMonth[icnm - 1];
                cnMonth[icnm] = "��" + cnMonth[icnm];
            }
            txcns += cnMonth[cnm];
            string[] cnDay ={ "", "��һ", "����", "����", "����", "����", "����", "����"
                , "����", "����", "��ʮ", "ʮһ", "ʮ��", "ʮ��"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   , "ʮ��", "ʮ��", "ʮ��"
                , "ʮ��", "ʮ��", "ʮ��", "��ʮ", "إһ", "إ��", "إ��", "إ��", "إ��"
                , "إ��", "إ��", "إ��", "إ��", "��ʮ" };
            txcns += cnDay[cnd];
            return txcns;
        }
Exemple #8
0
 /// <summary> 
 /// 获得农历 
 /// </summary> 
 /// <returns>http://img1.eqingyu.com</returns> 
 public static string cDate(DateTime dt)
 {
     ChineseLunisolarCalendar l = new ChineseLunisolarCalendar();
     //DateTime dt = DateTime.Today; //转换当日的日期  //
     // dt = new DateTime(2006, 1,29);
     //农历2006年大年初一(测试用),也可指定日期转换
     string[] aMonth ={ "", "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "腊月", "腊月" };  //a10表示日期的十位!
     string[] a10 ={ "初", "十", "廿", "卅" }; string[] aDigi ={ "O", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
     string sYear = "", sYearArab = "", sMonth = "", sDay = "", sDay10 = "", sDay1 = "", sLuniSolarDate = "";
     int iYear, iMonth, iDay; iYear = l.GetYear(dt); iMonth = l.GetMonth(dt); iDay = l.GetDayOfMonth(dt);  //Format Year
     sYearArab = iYear.ToString();
     for (int i = 0; i < sYearArab.Length; i++)
     {
         sYear += aDigi[Convert.ToInt16(sYearArab.Substring(i, 1))];
     }
     //Format Month
     int iLeapMonth = l.GetLeapMonth(iYear);
     //获取闰月
     if (iLeapMonth > 0 && iMonth <= iLeapMonth)
     {
         //
         for (int i = iLeapMonth + 1; i < 13; i++)
         {
             aMonth[i] = aMonth[i - 1];
             aMonth[iLeapMonth] = "闰" + aMonth[iLeapMonth - 1];
             sMonth = aMonth[l.GetMonth(dt)];
         }
     }
     else if (iLeapMonth > 0 && iMonth > iLeapMonth)
     {
         sMonth = aMonth[l.GetMonth(dt) - 1];
     }
     else
     {
         sMonth = aMonth[l.GetMonth(dt)];
     }
     //Format Day
     sDay10 = a10[iDay / 10];
     sDay1 = aDigi[(iDay % 10)];
     sDay = sDay10 + sDay1;
     if (iDay == 10) sDay = "初十";
     if (iDay == 20) sDay = "二十";
     if (iDay == 30) sDay = "三十";
     //Format Lunar Date  //
     sLuniSolarDate = dt.Year + "年" + dt.Month + "月" + dt.Day + "日 " + Weeks(dt) + " 农历" + sYear + "年" + sMonth + sDay;
     sLuniSolarDate = "" + sMonth + sDay;
     return sLuniSolarDate;
 }