C#实现带阴历显示的日期代码

本文实例讲述了C#实现带阴历显示的日期代码,分享给大家供大家参考。具体方法如下:

这是一个用于酒店预定功能的带日期控件,类似去哪网酒店预定,由于需要设置节假日不同时期内的价格,因此需要自己写个时间控件。在此分享下写时间控件过程中用到的农历显示类。

复制代码 代码如下:
public class CnCalendar
{
static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();
public static string GetChineseDateTime(DateTime datetime)
{
string strDate = datetime.Month + "月" + datetime.Day + "日";
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--;
}
}
if (strDate == "1月1日")
{
return "元旦";
}
else if (strDate == "2月14日")
{
return "情人节";
}
else if (strDate == "3月8日")
{
return "妇女节";
}
else if (strDate == "3月12日")
{
return "植树节";
}
else if (strDate == "4月1日")
{
return "愚人节";
}
else if (strDate == "5月1日")
{
return "劳动节";
}
else if (strDate == "5月4日")
{
return "青年节";
}
else if (strDate == "6月1日")
{
return "儿童节";
}
else if (strDate == "8月1日")
{
return "建军节";
}
else if (strDate == "9月10日")
{
return "教师节";
}
else if (strDate == "10月1日")
{
return "国庆节";
}
else
{
if (lday == 1)
{
return "" +
string.Concat(isleap ? "闰" : string.Empty, GetLunisolarMonth(lmonth), "月") + "
";
}
else
{
string strNongli = string.Concat(isleap ? "闰" : string.Empty, GetLunisolarMonth(lmonth), "月",
GetLunisolarDay(lday));
switch (strNongli)
{
case "正月初一":
return "春节";
case "正月十五":
return "元宵节";
case "五月初五":
return "端午节";
case "七月初七":
return "七夕";
case "八月十五":
return "中秋节";
case "九月初九":
return "重阳节";
case "腊月初八":
return "腊八节";
case "腊月二十四":
return "扫房节";
default:
return "" + GetLunisolarDay(lday) + "";
}
}
}
}

#region 农历年
///


/// 十天干
///

private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

///


/// 十二地支
///

private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };

///


/// 十二生肖
///

private static string[] shengxiao = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

///


/// 返回农历天干地支年
///

/// 农历年
///
public static string GetLunisolarYear(int year)
{
if (year > 3)
{
int tgIndex = (year - 4) % 10;
int dzIndex = (year - 4) % 12;

return string.Concat(tiangan[tgIndex], dizhi[dzIndex], "[", shengxiao[dzIndex], "]");

}

throw new ArgumentOutOfRangeException("无效的年份!");
}
#endregion

#region 农历月

///


/// 农历月
///

private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊月" };


///


/// 返回农历月
///

/// 月份
///
public static string GetLunisolarMonth(int month)
{
if (month < 13 && month > 0)
{
return months[month - 1];
}

throw new ArgumentOutOfRangeException("无效的月份!");
}


#endregion

#region 农历日

///


///
///

private static string[] days1 = { "初", "十", "廿", "三" };

///


/// 日
///

private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };


///


/// 返回农历日
///

///
///
public static string GetLunisolarDay(int day)
{
if (day > 0 && day < 32)
{
if (day != 20 && day != 30)
{
return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
}
else
{
return string.Concat(days[(day - 1) / 10], days1[1]);
}
}

throw new ArgumentOutOfRangeException("无效的日!");
}

#endregion

///


/// 返回生肖
///

/// 公历日期
///
public static string GetShengXiao(DateTime datetime)
{
return shengxiao[cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(datetime)) - 1];
}
}

希望本文所述对大家的C#程序设计有所帮助。

你可能感兴趣的:(C#实现带阴历显示的日期代码)