欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-16 09:50:34
本文实例讲述了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#程序设计有所帮助。

上一篇:

下一篇: