/// <summary>
 /// 返回指定公历日期的阴历时间
 /// </summary>
 /// <param name="time"></param>
 public ChinaDateTime(DateTime time)
 {
     cc = new System.Globalization.ChineseLunisolarCalendar();
     if (time > cc.MaxSupportedDateTime || time < cc.MinSupportedDateTime)
         throw new Exception("参数日期时间不在支持的范围内,支持范围:" + cc.MinSupportedDateTime.ToShortDateString() + "到" + cc.MaxSupportedDateTime.ToShortDateString());
     year = cc.GetYear(time);
     month = cc.GetMonth(time);
     if (month > 12)
         month -= 12;
     dayOfMonth = cc.GetDayOfMonth(time);
     isLeap = cc.IsLeapMonth(year, month);
     if (isLeap) month -= 1;
     this.time = time;
 }
        /// <summary>
        /// 返回指定农历年,月,日,是否为闰月的农历日期时间
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="Month"></param>
        /// <param name="DayOfMonth"></param>
        /// <param name="IsLeap"></param>
        public ChinaDateTime(int Year, int Month, int DayOfMonth)
        {
            cc = new System.Globalization.ChineseLunisolarCalendar();
            if (Year >= cc.MaxSupportedDateTime.Year || Year <= cc.MinSupportedDateTime.Year)
                throw new Exception("参数年份时间不在支持的范围内,支持范围:" + cc.MinSupportedDateTime.ToShortDateString() + "到" + cc.MaxSupportedDateTime.ToShortDateString());

            if (Month < 1 || Month > 12)
                throw new Exception("月份必须在1~12范围");
            cc = new System.Globalization.ChineseLunisolarCalendar();

            if (cc.GetDaysInMonth(Year, cc.IsLeapMonth(Year, Month) ? Month + 1 : Month) < DayOfMonth || DayOfMonth < 1)
                throw new Exception("指定的月中的天数不在当前月天数有效范围");
            year = Year;
            month = Month;
            dayOfMonth = DayOfMonth;
            isLeap = IsLeap;
            time = DateTime.Now;
        }
Example #3
0
    // int GetMonth (DateTime time)获取指定公历日期的农历月份
    public string GetMonth(DateTime time)
    {
        int month = calendar.GetMonth(time);
        int year  = calendar.GetYear(time);
        int leap  = 0;

        //正月不可能闰月
        for (int i = 3; i <= month; i++)
        {
            if (calendar.IsLeapMonth(year, i))
            {
                leap = i;
                break;  //一年中最多有一个闰月
            }
        }
        if (leap > 0)
        {
            month--;
        }
        return((leap == month + 1 ? "闰" : "") + ChineseMonthName[month - 1]);
    }