C#完成帶陰歷顯示的日期代碼。本站提示廣大學習愛好者:(C#完成帶陰歷顯示的日期代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是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 "<em class='calendarNLJieri'>除夕</em>";
}
else if (strDate == "2月14日")
{
return "<em class='calendarNLJieri'>戀人節</em>";
}
else if (strDate == "3月8日")
{
return "<em class='calendarNLJieri'>婦女節</em>";
}
else if (strDate == "3月12日")
{
return "<em class='calendarNLJieri'>植樹節</em>";
}
else if (strDate == "4月1日")
{
return "<em class='calendarNLJieri'>哲人節</em>";
}
else if (strDate == "5月1日")
{
return "<em class='calendarNLJieri'>休息節</em>";
}
else if (strDate == "5月4日")
{
return "<em class='calendarNLJieri'>青年節</em>";
}
else if (strDate == "6月1日")
{
return "<em class='calendarNLJieri'>兒童節</em>";
}
else if (strDate == "8月1日")
{
return "<em class='calendarNLJieri'>建軍節</em>";
}
else if (strDate == "9月10日")
{
return "<em class='calendarNLJieri'>教員節</em>";
}
else if (strDate == "10月1日")
{
return "<em class='calendarNLJieri'>國慶節</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'>春節</em>";
case "正月十五":
return "<em class='calendarNLJieri'>元宵節</em>";
case "蒲月初五":
return "<em class='calendarNLJieri'>端五節</em>";
case "七月初七":
return "<em class='calendarNLJieri'>七夕</em>";
case "八月十五":
return "<em class='calendarNLJieri'>中秋節</em>";
case "九月初九":
return "<em class='calendarNLJieri'>重陽節</em>";
case "尾月初八":
return "<em class='calendarNLJieri'>臘八節</em>";
case "尾月二十四":
return "<em class='calendarNLJieri'>掃房節</em>";
default:
return "<em class='calendarNL'>" + GetLunisolarDay(lday) + "</em>";
}
}
}
}
#region 陰歷年
/// <summary>
/// 十天干
/// </summary>
private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
/// <summary>
/// 十二地支
/// </summary>
private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
/// <summary>
/// 十二生肖
/// </summary>
private static string[] shengxiao = { "鼠", "牛", "虎", "免", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };
/// <summary>
/// 前往陰歷天干地支年
/// </summary>
/// <param name="year">陰歷年</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 陰歷月
/// <summary>
/// 陰歷月
/// </summary>
private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "尾月" };
/// <summary>
/// 前往陰歷月
/// </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 陰歷日
/// <summary>
///
/// </summary>
private static string[] days1 = { "初", "十", "廿", "三" };
/// <summary>
/// 日
/// </summary>
private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
/// <summary>
/// 前往陰歷日
/// </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#法式設計有所贊助。