国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現帶陰歷顯示的日期代碼

2020-01-24 02:21:30
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 额尔古纳市| 通榆县| 秦皇岛市| 昌平区| 洪雅县| 玛曲县| 游戏| 南澳县| 清新县| 洛宁县| 大城县| 左云县| 叶城县| 利津县| 彰化市| 汤阴县| 广水市| 乐安县| 德阳市| 长乐市| 沧州市| 宁陕县| 徐州市| 泉州市| 项城市| 惠水县| 西青区| 万荣县| 简阳市| 博爱县| 宁夏| 台中县| 广饶县| 嘉定区| 平山县| 丰镇市| 安图县| 辛集市| 龙州县| 商南县| 泽州县|