公司近段時間需要做一個帶日期控件的酒店預定功能,類似去哪網酒店預定,由于需要設置節假日不同時期內的價格,因此需要自己寫個時間控件。在此分享下寫時間控件過程中用到的農歷顯示類。
由于是第一次在博客園寫東西,不知道最終顯示格式會怎樣,見諒!

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]; } }
判斷節日不知道各位有沒有好的意見
新聞熱點
疑難解答