C#: 公历转中国农历
农历是中国现行的传统历法(农历和阴历是有区别的哦),是根据月相的变化周期来计算时间。
与公历不同,农历的年份分为平年和闰年,平年为十二个月,闰年为十三个月,月份分为大月和小月,大月三十天,小月二十九天。
在.net中提供了ChineseLunisolarCalendar实现公历转农历算法,下面是一个C#示例代码,完整的展示了如何将公历转换为农历:
/// <summary>
/// 获取中国农历
/// </summary>
public class DateHelper
{
///<summary>
/// 实例化
///</summary>
private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
///<summary>
/// 十天干
///</summary>
private static string[] TG = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
///<summary>
/// 十二地支
///</summary>
private static string[] DZ = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
///<summary>
/// 十二生肖
///</summary>
private static string[] SX = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
///<summary>
/// 农历月
///</summary>
///<return></return s>
private static string[] MONTHS = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };
///<summary>
/// 农历日
///</summary>
private static string[] DYAS1 = { "初", "十", "廿", "三" };
///<summary>
/// 农历日
///</summary>
private static string[] DAYS2 = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
///<summary>
/// 返回农历天干地支年
///</summary>
///<param name="year">农历年</param>
///<return>甲子[鼠]</return s>
public static string GetLunisolarYear(int year)
{
if (year > 3)
{
int tgIndex = (year - 4) % 10;
int dzIndex = (year - 4) % 12;
return string.Concat(TG[tgIndex], DZ[dzIndex], "[", SX[dzIndex], "]");
}
throw new ArgumentOutOfRangeException("无效的年份!");
}
///<summary>
/// 返回农历月
///</summary>
///<param name="month">月份</param>
///<return>十二(腊)</return s>
public static string GetLunisolarMonth(int month)
{
if (month < 13 && month > 0)
{
return MONTHS[month - 1];
}
throw new ArgumentOutOfRangeException("无效的月份!");
}
///<summary>
/// 返回农历日
///</summary>
///<param name="day">天</param>
///<return>初一</return s>
public static string GetLunisolarDay(int day)
{
if (day > 0 && day < 32)
{
if (day != 20 && day != 30)
{
return string.Concat(DYAS1[(day - 1) / 10], DAYS2[(day - 1) % 10]);
}
else
{
return string.Concat(DAYS2[(day - 1) / 10], DYAS1[1]);
}
}
throw new ArgumentOutOfRangeException("无效的日!");
}
///<summary>
/// 根据公历获取农历日期
///</summary>
///<param name="datetime">公历日期</param>
///<return>农历一月初一</return s>
public static string GetChineseDate(DateTime datetime)
{
int year = ChineseCalendar.GetYear(datetime);
int month = ChineseCalendar.GetMonth(datetime);
int day = ChineseCalendar.GetDayOfMonth(datetime);
//获取闰月, 0 则表示没有闰月
int leapMonth = ChineseCalendar.GetLeapMonth(year);
//如果有闰月,之后都需要减月
if (leapMonth > 0)
{
if (month >= leapMonth)
{
//闰月
month--;
}
}
return string.Concat("农历", GetLunisolarMonth(month), "月", GetLunisolarDay(day));
}
///<summary>
/// 根据公历获取农历日期
///</summary>
///<param name="datetime">公历日期</param>
///<return>甲子[鼠]闰一月初一</return s>
public static string GetChineseDateTime(DateTime datetime)
{
int year = ChineseCalendar.GetYear(datetime);
int month = ChineseCalendar.GetMonth(datetime);
int day = ChineseCalendar.GetDayOfMonth(datetime);
//获取闰月, 0 则表示没有闰月
int leapMonth = ChineseCalendar.GetLeapMonth(year);
bool isleap = false;
if (leapMonth > 0)
{
if (leapMonth == month)
{
//闰月
isleap = true;
month--;
}
else if (month > leapMonth)
{
month--;
}
}
return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
}
}