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#程序设计有所帮助。
猜你喜欢
- 修改整理的一个通用类,用来操作oracle数据库 十分的方便,支持直接操作sql语句和Hash表操作.现在修补MIS我都用这个类,节约了大
- Spring Boot+Vue 前后端分离项目架构项目流程:1. SpringBoot 后端项目1、新建一个 SpringBoot 工程,并
- 在WPF中,TreeView默认情况是不支持右键选定的,也就是说,当右键点击某节点时,是无法选中该节点的。当我们想在TreeViewItem
- private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAd
- 1. 服务端配置java启动命令增加以下参数即可:-Dcom.sun.management.jmxremote \-Dcom.sun.man
- 前言之前我们探讨过一个.class文件是如何被加载到jvm中的。但是jvm内又是如何划分内存的呢?这个内被加载到了那一块内存中?jvm内存划
- package com.chase.test;import java.util.ArrayList;import java.util.Has
- 1 什么是class对象类是程序的一部分,每个类都有一个class对象。换言之,每当编写并且编译了一个新类,就会产生一个class对象(更恰
- 目录栈溢出(虚拟机栈和本地方法栈)产生原因解决思路堆溢出产生原因解决思路方法区和运行时常量池溢出产生原因解决思路本机直接内存溢出产生原因解决
- hbase是运行在Hadoop上的NoSQL数据库,它是一个分布式的和可扩展的大数据仓库,也就是说HBase能够利用HDFS的分布式处理模式
- 类:NameII 权限:public方法:main 权限:publi
- 前言『 * 』其实源于设计模式中的代理模式,而代理模式就是使用代理对象完成用户请求,屏蔽用户对真实对象的访问。举个最简单的例子,比如我们想
- 本文实例为大家分享了java实现捕鱼达人游戏的具体代码,供大家参考,具体内容如下效果图如下:源代码分享:测试类:package game;i
- 1. 简单说明嗨,大家好!今天给大家分享的是Mybatis-plus 插件的分页机制,说起分页机制,相信我们程序员都不陌生,今天,我就给大家
- 业务场景近年来B2C、O2O等商业概念的提出和移动端的发展,使得分布式系统流行了起来。分布式系统相对于单系统,解决了流量大、系统高可用和高容
- springmvc dao层和service层的区别首先解释面上意思,service是业务层,dao是数据访问层这个问题我曾经也有过,记得以
- 虽然GUI技术没有很大的市场,甚至很多初学者放弃学习GUI,但是学习GUI编程的过程对于提高编程兴趣,深入理解Java编程有很大的作用。效果
- 一个非侵入的api编译、收集、Rest文档生成工具。工具通过分析代码和注释,获取文档信息,生成RestDoc文档前言程序员一直以来都有一个烦
- 这篇文章主要介绍了spring web.xml指定配置文件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价
- Unix时间戳最小单位是秒,开始时间为格林威治标准时间1970-01-01 00:00:00ConvertIntDateTime方法的基本思