Exemple #1
0
        /// <summary>
        /// 将  农历(阴历)   转换为   公历(阳历,西历)
        ///    如果传入的参数中的年份是润年,需要另外进行加上润月的天数,简单测试,但没有做过多的测试,不知是否完全正确
        ///
        ///    DateTime dt = Convert.ToDateTime("2010-05-21 00:00:00");
        ///    MessageBox.Show(calendarChineseLunisolarToSolar(dt).ToString());
        ///
        /// </summary>
        ///
        public static DateTime calendarChineseLunisolarToSolar(DateTime ChineseLunisolarDateTime)
        {
            System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();

            if (ChineseLunisolarDateTime.Year < 1902 || ChineseLunisolarDateTime.Year > 2100)
            {
                throw new Exception("只支持1902〜2100期间的农历年");
            }

            DateTime dt = cal.ToDateTime(ChineseLunisolarDateTime.Year, ChineseLunisolarDateTime.Month, ChineseLunisolarDateTime.Day, 0, 0, 0, 0);

            //检测是否含有润月
            int leapMonth = cal.GetLeapMonth(ChineseLunisolarDateTime.Year);

            int leapMonthInDays = 0;

            if (leapMonth > 0)
            {
                //有润月,则读到这个润月里面的天数
                leapMonthInDays = cal.GetDaysInMonth(ChineseLunisolarDateTime.Year, ChineseLunisolarDateTime.Month);
            }

            dt = dt.AddDays(leapMonthInDays);

            return(dt);
        }
        /// <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, bool IsLeap)
        {
            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.GetLeapMonth(Year) != Month && IsLeap)
            {
                throw new Exception("指定的月份不是当年的闰月");
            }
            if (cc.GetDaysInMonth(Year, IsLeap ? Month + 1 : Month) < DayOfMonth || DayOfMonth < 1)
            {
                throw new Exception("指定的月中的天数不在当前月天数有效范围");
            }
            year       = Year;
            month      = Month;
            dayOfMonth = DayOfMonth;
            isLeap     = IsLeap;
            time       = DateTime.Now;
        }
        /// <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, bool IsLeap)
        {
            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.GetLeapMonth(Year) != Month && IsLeap)
                throw new Exception("指定的月份不是当年的闰月");
            if (cc.GetDaysInMonth(Year, IsLeap ? Month + 1 : Month) < DayOfMonth || DayOfMonth < 1)
                throw new Exception("指定的月中的天数不在当前月天数有效范围");
            year = Year;
            month = Month;
            dayOfMonth = DayOfMonth;
            isLeap = IsLeap;
            time = DateTime.Now;
        }