#region 阴历转阳历 /// <summary> /// 获取指定年份春节当日(正月初一)的阳历日期 /// </summary> /// <param name="year">指定的年份</param> private DateTime GetLunarNewYearDate(int year) { DateTime dt = new DateTime(year, 1, 1); int cnYear = calendar.GetYear(dt); int cnMonth = calendar.GetMonth(dt); int num1 = 0; int num2 = calendar.IsLeapYear(cnYear) ? 13 : 12; while (num2 >= cnMonth) { num1 += calendar.GetDaysInMonth(cnYear, num2--); } num1 = num1 - calendar.GetDayOfMonth(dt) + 1; return dt.AddDays(num1); } /// <summary> /// 阴历转阳历 /// </summary> /// <param name="year">阴历年</param> /// <param name="month">阴历月</param> /// <param name="day">阴历日</param> /// <param name="IsLeapMonth">是否闰月</param> public DateTime GetDateFromLunarDate(int year, int month, int day, bool IsLeapMonth) { if (year < 1902 || year > 2100) throw new Exception("只支持1902~2100期间的农历年"); if (month < 1 || month > 12) throw new Exception("表示月份的数字必须在1~12之间"); if (day < 1 || day > calendar.GetDaysInMonth(year, month)) throw new Exception("农历日期输入有误"); int num1 = 0, num2 = 0; int leapMonth = calendar.GetLeapMonth(year); if (((leapMonth == month + 1) && IsLeapMonth) || (leapMonth > 0 && leapMonth <= month)) num2 = month; else num2 = month - 1; while (num2 > 0) { num1 += calendar.GetDaysInMonth(year, num2--); } DateTime dt = GetLunarNewYearDate(year); return dt.AddDays(num1 + day - 1); } /// <summary> /// 阴历转阳历 /// </summary> /// <param name="date">阴历日期</param> /// <param name="IsLeapMonth">是否闰月</param> public DateTime GetDateFromLunarDate(DateTime date, bool IsLeapMonth) { return GetDateFromLunarDate(date.Year, date.Month, date.Day, IsLeapMonth); } #endregion #region 阳历转阴历 public 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); return string.Format("农历{0}{1}({2})年{3}{4}月{5}{6}" , "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10] , "子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12] , "鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12] , month == leapMonth ? "闰" : "" , "无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month] , "初十廿三"[day / 10] , "日一二三四五六七八九"[day % 10] ); } #endregion
阳历转阴历
#region 阳历转阴历 public 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); return string.Format("农历{0}{1}({2})年{3}{4}月{5}{6}" , "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10] , "子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12] , "鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12] , month == leapMonth ? "闰" : "" , "无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month] , "初十廿三"[day / 10] , "日一二三四五六七八九"[day % 10] ); } #endregion
调用方法:
//阴历转阳历 public void GetDayFromLunarDate(DateTime time) { ChineseCalendar cc = new ChineseCalendar(time); cc.GetDateFromLunarDate(time, true); } //阳历转阴历 public void GetLunarDateFromDay(DateTime time) { ChineseCalendar cc = new ChineseCalendar(time); string LunarDate = cc.SolarToChineseLunisolarDate(time); }
最新评论