C#實現(xiàn)帶陰歷顯示的日期代碼
本文實例講述了C#實現(xiàn)帶陰歷顯示的日期代碼,分享給大家供大家參考。具體方法如下:
這是一個用于酒店預定功能的帶日期控件,類似去哪網(wǎng)酒店預定,由于需要設置節(jié)假日不同時期內(nèi)的價格,因此需要自己寫個時間控件。在此分享下寫時間控件過程中用到的農(nóng)歷顯示類。
{
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 "<em class='calendarNLJieri'>元旦</em>";
}
else if (strDate == "2月14日")
{
return "<em class='calendarNLJieri'>情人節(jié)</em>";
}
else if (strDate == "3月8日")
{
return "<em class='calendarNLJieri'>婦女節(jié)</em>";
}
else if (strDate == "3月12日")
{
return "<em class='calendarNLJieri'>植樹節(jié)</em>";
}
else if (strDate == "4月1日")
{
return "<em class='calendarNLJieri'>愚人節(jié)</em>";
}
else if (strDate == "5月1日")
{
return "<em class='calendarNLJieri'>勞動節(jié)</em>";
}
else if (strDate == "5月4日")
{
return "<em class='calendarNLJieri'>青年節(jié)</em>";
}
else if (strDate == "6月1日")
{
return "<em class='calendarNLJieri'>兒童節(jié)</em>";
}
else if (strDate == "8月1日")
{
return "<em class='calendarNLJieri'>建軍節(jié)</em>";
}
else if (strDate == "9月10日")
{
return "<em class='calendarNLJieri'>教師節(jié)</em>";
}
else if (strDate == "10月1日")
{
return "<em class='calendarNLJieri'>國慶節(jié)</em>";
}
else
{
if (lday == 1)
{
return "<em class='calendarNL'>" +
string.Concat(isleap ? "閏" : string.Empty, GetLunisolarMonth(lmonth), "月") + "</em>";
}
else
{
string strNongli = string.Concat(isleap ? "閏" : string.Empty, GetLunisolarMonth(lmonth), "月",
GetLunisolarDay(lday));
switch (strNongli)
{
case "正月初一":
return "<em class='calendarNLJieri'>春節(jié)</em>";
case "正月十五":
return "<em class='calendarNLJieri'>元宵節(jié)</em>";
case "五月初五":
return "<em class='calendarNLJieri'>端午節(jié)</em>";
case "七月初七":
return "<em class='calendarNLJieri'>七夕</em>";
case "八月十五":
return "<em class='calendarNLJieri'>中秋節(jié)</em>";
case "九月初九":
return "<em class='calendarNLJieri'>重陽節(jié)</em>";
case "臘月初八":
return "<em class='calendarNLJieri'>臘八節(jié)</em>";
case "臘月二十四":
return "<em class='calendarNLJieri'>掃房節(jié)</em>";
default:
return "<em class='calendarNL'>" + GetLunisolarDay(lday) + "</em>";
}
}
}
}
#region 農(nóng)歷年
/// <summary>
/// 十天干
/// </summary>
private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
/// <summary>
/// 十二地支
/// </summary>
private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
/// <summary>
/// 十二生肖
/// </summary>
private static string[] shengxiao = { "鼠", "牛", "虎", "免", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };
/// <summary>
/// 返回農(nóng)歷天干地支年
/// </summary>
/// <param name="year">農(nóng)歷年</param>
/// <returns></returns>
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 農(nóng)歷月
/// <summary>
/// 農(nóng)歷月
/// </summary>
private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "臘月" };
/// <summary>
/// 返回農(nóng)歷月
/// </summary>
/// <param name="month">月份</param>
/// <returns></returns>
public static string GetLunisolarMonth(int month)
{
if (month < 13 && month > 0)
{
return months[month - 1];
}
throw new ArgumentOutOfRangeException("無效的月份!");
}
#endregion
#region 農(nóng)歷日
/// <summary>
///
/// </summary>
private static string[] days1 = { "初", "十", "廿", "三" };
/// <summary>
/// 日
/// </summary>
private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
/// <summary>
/// 返回農(nóng)歷日
/// </summary>
/// <param name="day"></param>
/// <returns></returns>
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
/// <summary>
/// 返回生肖
/// </summary>
/// <param name="datetime">公歷日期</param>
/// <returns></returns>
public static string GetShengXiao(DateTime datetime)
{
return shengxiao[cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(datetime)) - 1];
}
}
希望本文所述對大家的C#程序設計有所幫助。
相關文章
C# WinForm狀態(tài)欄實時顯示當前時間(窗體狀態(tài)欄StatusStrip示例)
這篇文章主要介紹了C# WinForm狀態(tài)欄實時顯示當前時間(窗體狀態(tài)欄StatusStrip示例),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01