In the. net2.0 version of System.Globalization.ChineseLunisolarCalendar is the Chinese calendar class, the Gregorian calendar and the Chinese traditional lunar calendar between the conversion, using it can calculate the celestial stems and earthly branches and other information about the lunar calendar.
Use
static Chineselunisolarcalendar Ccalendar = new Chineselunisolarcalendar ();
Ccalendar.maxsupporteddatetime returns the maximum supported date, that is, 2101-1-28
Ccalendar.minsupporteddatetime returns the minimum supported date, that is, 1901-2-19
Here we will implement the Gregorian calendar to the lunar calendar.
/// <summary>
/// 根据公历获取农历日期
/// </summary>
/// <param name="datetime">公历日期</param>
/// <returns></returns>
public static string GetChineseDateTime(DateTime datetime)
{
int lyear = cCalendar.GetYear(datetime);
int lmonth = cCalendar.GetMonth(datetime);
int lday = cCalendar.GetDayOfMonth(datetime);
//获取闰月, 0 则表示没有闰月
int leapMonth = cCalendar.GetLeapMonth(lyear);
bool isleap = false;
if (leapMonth > 0)
{
if (leapMonth == lmonth)
{
//闰月
isleap = true;
lmonth--;
}
else if (lmonth > leapMonth)
{
lmonth--;
}
}
return string.Concat(GetLunisolarYear(lyear), "年", isleap ? " 闰" : string.Empty, GetLunisolarMonth(lmonth), "月", GetLunisolarDay (lday));
}
Results of the test:
Incoming Date: 2010-3-4
Return to the Lunar New Year: the first month of 19
Can satisfy the simple demand.