C#实现带阴历显示的日期代码
作者:shichen2014 发布时间:2023-12-11 02:19:04
本文实例讲述了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#程序设计有所帮助。


猜你喜欢
- <?xml version="1.0" encoding="utf-8"?> <L
- 1.概述最近一直都在带实习生做项目,发现自己好久没有写博客了,这几天更新会比较频繁,今天玩QQ的时候发现QQ主页菜单滑动效果早就变了,实在忍
- Android 6.0版本对于程序员兄弟来说最不友好的就是权限的问题,动态权限的设置曾经让我很苦恼,目前大部分关于6.0权限设置的框架基本都
- springmvc 自定义注解 以及自定义注解的解析一、自定义注解名字@Target({ElementType.TYPE, ElementT
- 本文实例为大家分享了Android读取NFC卡的编号具体代码,供大家参考,具体内容如下NFC相关androidManifest文件设置:一、
- 前段时间接到一个Web应用自动生成Word的需求,现整理了下一些关键步骤拿来分享一下。思路:(注:这里只针对WORD2003版本,其它版本大
- 1. 什么是静态内部类在Java中有静态代码块、静态变量、静态方法,当然也有静态类,但Java中的静态类只能是Java的内部类,也称为静态嵌
- 屏幕切换指的是在同一个Activity内屏幕间的切换,ViewFlipper继承了Framelayout类,ViewAnimator类的作用
- SpringBatch介绍:SpringBatch 是一个大数据量的并行处理框架。通常用于数据的离线迁移,和数据处理,⽀持事务、并发、流程、
- TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计。一、基础知识TabWidget : 该组件就是TabHost
- 简介记录一个利用系统分享功能进行图片分享的工具类(代码是用Kotlin写的,都是比较简单的语法,部分可能需要自定义的地方都已经标出)。调用方
- 如果不考虑更深层的性能问题,我个人认为ScrollerView还是很好用的。而且单用ScrollerView就可以实现分类型的Recycle
- 死锁问题死锁定义多线程编程中,因为抢占资源造成了线程无限等待的情况,此情况称为死锁。死锁举例注意:线程和锁的关系是:一个线程可以拥有多把锁,
- java生成json隐藏关键属性今天在工作中遇到一个这样的问题,当后端返回数据时一些关键信息或敏感信息并不想返回到前端,但是又懒得定义专用的
- net 原则上禁止跨线程访问控件,因为这样可能造成错误的发生,推荐的解决方法是采用代理,用代理方法来间接操作不是同一线程创建的控件。第二种方
- Bottom SheetBottom Sheet 是 Design Support Library 23.2 版本引入的一个类似于对话框的控
- 作用:1、定时执行某种功能2、重复执行、定时重复执行、定次数执行某种功能类别:1、 Thread(new Runnable)2、T
- 本文实例为大家分享了Android实现图片查看器的具体代码,供大家参考,具体内容如下效果需要两个手指禁止缩放,所以没有光标,只能用手机投放电
- 内存分配方式简介在C++中,内存分成5个区,他们分别是堆、栈、自由存储区、全局/静态存储区和常量存储区。栈:在执行函数时,函数内局部变量的存
- 前言我们在日常的开发过程中针对一些字段采用整型的方式去代替某些具体的含义,比如性别0代表男,1代表女。如果只是一些不会变更的转译我们可以采用