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

所属分类: 软件编程 / C#教程 阅读数: 88
收藏 0 赞 0 分享

本文实例讲述了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#程序设计有所帮助。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多