网站上显示农历及阳历节日

news/2024/5/20 7:08:38/文章来源:https://blog.csdn.net/weixin_30487701/article/details/99814256

把阳历日期转换为农历用到的函数为ChinaDate.ConvertToNongLi(DateTime.Now);

得到节日的函数为 ChinaDate.GetFestival(DateTime.Now);

2011030517404033.jpg

代码:

using System.Globalization;
using System.Collections;
using System;namespace NetWeb2011.Common
{public static class ChinaDate{private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar();private static Hashtable gHoliday = new Hashtable();private static Hashtable nHoliday = new Hashtable();private static string[] JQ = { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };private static int[] JQData = { 0, 21208, 43467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };static ChinaDate(){//公历节日gHoliday.Add("0101", "元旦");gHoliday.Add("0214", "情人节");gHoliday.Add("0305", "雷锋日");gHoliday.Add("0308", "妇女节");gHoliday.Add("0312", "植树节");gHoliday.Add("0315", "消费者权益日");gHoliday.Add("0401", "愚人节");gHoliday.Add("0405", "清明节");gHoliday.Add("0501", "劳动节");gHoliday.Add("0504", "青年节");gHoliday.Add("0601", "儿童节");gHoliday.Add("0701", "建党节");gHoliday.Add("0801", "建军节");gHoliday.Add("0910", "教师节");gHoliday.Add("1001", "国庆节");gHoliday.Add("1031", "万圣节");gHoliday.Add("1224", "平安夜");gHoliday.Add("1225", "圣诞节");//农历节日nHoliday.Add("0101", "春节");nHoliday.Add("0115", "元宵节");nHoliday.Add("0505", "端午节");nHoliday.Add("0815", "中秋节");nHoliday.Add("0909", "重阳节");nHoliday.Add("1208", "腊八节");nHoliday.Add("1230", "大年三十");}/// <summary>/// 获取农历/// </summary>/// <param name="dt"></param>/// <returns></returns>public static string GetChinaDate(DateTime dt){if (dt > china.MaxSupportedDateTime || dt < china.MinSupportedDateTime){//日期范围:1901 年 2 月 19 日 - 2101 年 1 月 28 日throw new Exception(string.Format("日期超出范围!必须在{0}到{1}之间!", china.MinSupportedDateTime.ToString("yyyy-MM-dd"), china.MaxSupportedDateTime.ToString("yyyy-MM-dd")));}string str = string.Format("{0} {1}{2}", GetYear(dt), GetMonth(dt), GetDay(dt));string strJQ = GetSolarTerm(dt);if (strJQ != ""){str += " (" + strJQ + ")";}string[] strHoliday = GetHoliday(dt, 0);if (strHoliday != null){str += " " + strHoliday;}string[] strChinaHoliday = GetChinaHoliday(dt, 0);if (strChinaHoliday != null){str += " " + strChinaHoliday;}return str;}/// <summary>/// 获取农历年份/// </summary>/// <param name="dt"></param>/// <returns></returns>public static string GetYear(DateTime dt){int yearIndex = china.GetSexagenaryYear(dt);string yearTG = " 甲乙丙丁戊己庚辛壬癸";string yearDZ = " 子丑寅卯辰巳午未申酉戌亥";string yearSX = " 鼠牛虎兔龙蛇马羊猴鸡狗猪";int year = china.GetYear(dt);int yTG = china.GetCelestialStem(yearIndex);int yDZ = china.GetTerrestrialBranch(yearIndex);string str = string.Format("[{1}]{2}{3}{0}", year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]);return str;}/// <summary>/// 得到公历所对应的农历年/// </summary>/// <param name="dt"></param>/// <returns></returns>public static string GetChinaYear(DateTime dt){return china.GetYear(dt).ToString();}/// <summary>/// 获取农历月份/// </summary>/// <param name="dt"></param>/// <returns></returns>public static string GetMonth(DateTime dt){int year = china.GetYear(dt);int iMonth = china.GetMonth(dt);int leapMonth = china.GetLeapMonth(year);bool isLeapMonth = iMonth == leapMonth;if (leapMonth != 0 && iMonth >= leapMonth){iMonth--;}string szText = "正二三四五六七八九十";string strMonth = isLeapMonth ? "闰" : "";if (iMonth <= 10){strMonth = "";strMonth = strMonth + szText.Substring(iMonth - 1, 1);}else if (iMonth == 11){strMonth = "十一";}else{strMonth = "腊";}return strMonth + "月";}/// <summary>/// 获取农历日期/// </summary>/// <param name="dt"></param>/// <returns></returns>public static string GetDay(DateTime dt){int iDay = china.GetDayOfMonth(dt);string szText1 = "初十廿三";string szText2 = "一二三四五六七八九十";string strDay;if (iDay == 20){strDay = "二十";}else if (iDay == 30){strDay = "三十";}else{strDay = szText1.Substring((iDay - 1) / 10, 1);strDay = strDay + szText2.Substring((iDay - 1) % 10, 1);}return strDay;}private static string GetNongLiDay(string str){string ret = string.Empty;ret += ConvertNongliToDigital(str.Substring(0, 1), 0);ret += ConvertNongliToDigital(str.Substring(1, 1), 1);return ret;}private static string GetNongLiMonth(string str){string ret = string.Empty;if (str.Length == 2) //是 十月 还是 十一月{ret += ConvertMonthToDigital(str.Substring(0, 1));ret += ConvertMonthToDigital(str.Substring(1, 1));}else if (str.Length == 3){ret += ConvertMonthToDigital(str.Substring(0, 2));}return ret;}/// <summary>/// 把汉字月转换成对应的数字/// </summary>/// <param name="nonglimonth"></param>/// <returns></returns>private static string ConvertMonthToDigital(string nonglimonth){string ret = string.Empty;switch (nonglimonth){case "一": ret = "1";break;case "二": ret = "2";break;case "三": ret = "3";break;case "四": ret = "4";break;case "五": ret = "5";break;case "六": ret = "6";break;case "七": ret = "7";break;case "八": ret = "8";break;case "九": ret = "9";break;case "十":ret = "10";break;case "十一":ret = "11";break;case "十二":ret = "12";break;case "正":ret = "1";break;case "腊":ret = "12";break;}return ret;}/// <summary>/// 把汉字日转换成对应的数字,first,是用来区别十这个汉字,是在第一个位置还是在别的位置上/// </summary>/// <param name="nongli"></param>/// <param name="first"></param>/// <returns></returns>private static string ConvertNongliToDigital(string nongli, int first){string ret = string.Empty;switch (nongli){case "一": ret = "1";break;case "二": ret = "2";break;case "三": ret = "3";break;case "四": ret = "4";break;case "五": ret = "5";break;case "六": ret = "6";break;case "七": ret = "7";break;case "八": ret = "8";break;case "九": ret = "9";break;case "十":if (first == 1){ret = "0";}else ret = "1";break;case "初": ret = "0";break;case "廿": ret = "2";break;}return ret;}/// <summary>/// 获取节气/// </summary>/// <param name="dt"></param>/// <returns></returns>public static string GetSolarTerm(DateTime dt){DateTime dtBase = new DateTime(1900, 1, 6, 2, 5, 0);DateTime dtNew;double num;int y;string strReturn = "";y = dt.Year;for (int i = 1; i <= 24; i++){num = 525948.76 * (y - 1900) + JQData[i - 1];dtNew = dtBase.AddMinutes(num);if (dtNew.DayOfYear == dt.DayOfYear){strReturn = JQ[i - 1];}}return strReturn;}/// <summary>/// 获取公历节日/// </summary>/// <param name="dt"></param>/// <returns></returns>public static string[] GetHoliday(DateTime dt, int next) //next为0表示当前月,next为1表示下个月{string strReturn = "";object g = null;if (next == 0){g = gHoliday[dt.Month.ToString("00") + dt.Day.ToString("00")];}if (g != null){strReturn = g.ToString();return new string[] { dt.Month.ToString("00") + dt.Day.ToString("00"), g.ToString() };}else{string first = string.Empty;int day = 0;//当前多少号if (next == 0){first = dt.Month.ToString("00");day = dt.Day; //当前多少号}else{dt = dt.AddMonths(next);first = dt.Month.ToString("00");}day++;for (int i = day; i <= 31; i++){object festival = gHoliday[first + i.ToString("00")];if (festival != null){string[] ret = { first + i.ToString("00"), festival.ToString() }; //当前当月的离今天最近的一个节日return ret;}}}return null;}/// <summary>///  获取农历节日/// </summary>/// <param name="dt"></param>/// <returns></returns>public static string[] GetChinaHoliday(DateTime dt, int next) //next表示是否是下一个月{string strReturn = string.Empty;int year = china.GetYear(dt);int iMonth = china.GetMonth(dt);int leapMonth = china.GetLeapMonth(year);int iDay = china.GetDayOfMonth(dt);if (china.GetDayOfYear(dt) == china.GetDaysInYear(year)){strReturn = "除夕";}else if (leapMonth != iMonth){if (leapMonth != 0 && iMonth >= leapMonth){iMonth--;}object n = nHoliday[iMonth.ToString("00") + iDay.ToString("00")];if (n != null){string[] ret = { iMonth.ToString("00") + iDay.ToString("00"), n.ToString() };return ret;}else  //该日期没有节日{if (next != 0){iDay = 0;iMonth += next;}for (int i = iDay; i <= 31; i++){object festival = nHoliday[iMonth.ToString("00") + i.ToString("00")];if (festival != null){string[] ret = { iMonth.ToString("00") + i.ToString("00"), festival.ToString() };return ret;}}}}if (strReturn != string.Empty){return new string[] { "1230", "除夕" };}return null;}/// <summary>/// 把阳历转成农历/// </summary>/// <param name="dt"></param>/// <returns></returns>public static DateTime ConvertToNongLi(DateTime dt){string year = ChinaDate.GetChinaYear(dt);string month = ChinaDate.GetNongLiMonth(ChinaDate.GetMonth(dt));string day = ChinaDate.GetNongLiDay(ChinaDate.GetDay(dt));DateTime dtnongli = DateTime.Parse(year + "-" + month + "-" + day);return dtnongli;}/// <summary>/// 得到节日,没有节日的话,就返回empty/// </summary>/// <param name="dt"></param>/// <returns></returns>public static string GetFestival(DateTime dt){try{string currentfestival = string.Empty;string nonglidate = string.Empty;string gonglidate = string.Empty;int distance = 0;int basemonth = 0;string[] gongli = ChinaDate.GetHoliday(dt, basemonth);//公历节日string[] nongli = ChinaDate.GetChinaHoliday(dt, basemonth);//农历节日bool festival = true; //是否这个月内有节日DateTime dtGongli = dt;DateTime dtNongli = dt;if (gongli == null && nongli == null) //如果同时为空,就取下个月的{basemonth++;gongli = ChinaDate.GetHoliday(dt, basemonth);//公历节日nongli = ChinaDate.GetChinaHoliday(dt, basemonth);//农历节日}//dt = dt.AddMonths(basemonth);if (gongli != null){DateTime riqi = DateTime.Parse(dt.Year.ToString() + "-" + gongli[0].Substring(0, 2) + "-" + gongli[0].Substring(2, 2));dtGongli = ConvertToNongLi(riqi);}if (nongli != null){dtNongli = DateTime.Parse(ChinaDate.GetChinaYear(dt) + "-" + nongli[0].Substring(0, 2) + "-" + nongli[0].Substring(2, 2));}System.Text.StringBuilder today = new System.Text.StringBuilder();//today.Append(DateTime.Now.ToString("yyyy-MM-dd") + " " + DateTime.Now.ToString("ddd", new System.Globalization.CultureInfo("zh-cn")) + "  ");if (gongli != null && nongli != null){TimeSpan ts = dtGongli - dtNongli;if (ts.Days < 0)// 公历的节日更近 把两个日期都转换成公历进行比较{currentfestival = gongli[1];gonglidate = gongli[0];TimeSpan tsDay = dtGongli - ConvertToNongLi(dt);distance = tsDay.Days;}else{ts = dtNongli - dtGongli;currentfestival = nongli[1];TimeSpan tsDay = dtNongli - ConvertToNongLi(dt);distance = tsDay.Days;}}else if (gongli != null){currentfestival = gongli[1];DateTime dtfestival = ConvertToNongLi(DateTime.Parse(ChinaDate.GetChinaYear(dt) + "-" + gongli[0].Substring(0, 2) + "-" + gongli[0].Substring(2, 2)));TimeSpan tsDay = dtfestival - ConvertToNongLi(dt);distance = tsDay.Days;}else if (nongli != null){currentfestival = nongli[1];distance = int.Parse(nongli[0].Substring(2, 2)) - ConvertToNongLi(dt).Day;}else festival = false;//today.Append("农历" + ChinaDate.GetMonth(dt) + ChinaDate.GetDay(dt));if (festival){if (distance > 0)today.Append(" 离<font color='red'>" + currentfestival + "</font>还有<font color='red'>" + distance + "</font>天   ");else today.Append(" 今天是 <font color='red'>" + currentfestival + "</font>   ");}//else today.Append("  ");return today.ToString();}catch{return string.Empty;}}}
}

转载于:https://www.cnblogs.com/lhking/archive/2011/03/05/1971732.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_810307.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AWWWB 网站克隆器 v1.0 发布

软件名称&#xff1a;AWWWB.COM网站克隆器1.0 开发商&#xff08;主页&#xff09;&#xff1a;www.awwwb.com 联系人&#xff1a;awwwb.comqq.com 软件性质&#xff1a;免费软件/开源软件 软件描述&#xff1a; AWWWB.COM网站克隆器&#xff1a;输入被克隆网站的首页网址&…

一个Java语言所写的shop网站框架明细

核心框架Spring Framework &#xff1a;作为一个优秀的开源框架&#xff0c;是为了解决企业应用程序开发复杂性而创建的。框架的主要优势之一就是其分层架构&#xff0c;分层架构允许您选择使用哪一个组件&#xff0c;同时为 J2EE 应用程序开发提供集成的框架。 模板引擎FreeM…

iframe懒加载_wordpress网站视频和图片懒加载插件的对比与使用 a3 Lazy Load

都说wordpress网站速度慢&#xff0c;三人成虎&#xff0c;养虎为患&#xff0c;今天就推荐个使wordpress网站提速度的插件。特别是图片或者视频较多的站&#xff0c;用个懒加载插件将大大减少一时的http(s)请求、减少服务器端压力&#xff0c;使服务器按需加载。对于用户体验&…

php教育网站设计案例_UI设计素材模板|设计良好的教育网站:3个快捷技巧

拥有一个高质量的学校网站比以往任何时候都更重要。优秀的UI设计素材模板&#xff0c;帮助设计师和管理人员建立最好的教育网站。从成功的设计中寻找灵感&#xff01;3个快捷技巧1. 使用教育专用的网站构建器首先&#xff0c;用专门教育内容管理系统(CMS)来制作学校网站是个好主…

服务器内部移动文件速度慢,网站打开速度慢的原因分析已经解决排查方案(实用大全)...

72018/7网站打开速度慢的原因分析已经解决排查方案(实用大全)飞鸟哥上百个案例&#xff0c;3天整理,实证案例&#xff0c;全面&#xff0c;实用&#xff01;假如你是网站提供者&#xff0c;或者网站运营者看下文本给你专业全面的经验。飞鸟哥&#xff0c;根据10年经验&#xff…

秒杀全网!研发、运营必备实用工具网站

目录 1、搜索引擎 2、PPT 3、图片操作 4、文件共享 5、招聘求职 6、程序员面试题库 7、办公、开发软件 8、高清图片、视频素材网站 9、项目开源 10、算法 11、在线工具宝典大全 12、音乐 13、神辅助工具 14、语音处理 15、大数据 16、电子书 程序员开发需要具…

【织梦插件】xenu软件-网站url和死链提取工具免费下载

软件名称xenu软件作用网站网址提取工具适宜人群SEO网址http://www.jingdouwang.cn/zygx/wzcj/278.html 软件简介&#xff1a; Xenu Link Sleuth 可能是你所见功能最强大的网站死链接查询的软件了。使用方法简单&#xff0c;仅需要输入网站URL就可以完成死链查询。用户可直接查看…

IIS部署,发布网站

因项目需要&#xff0c;正在学习如何部署IIS服务&#xff0c;发布网站&#xff0c;将遇到的问题记录下。 一、IIS部署 1.打开控制面板&#xff0c;选择 ‘程序’ 2.程序和功能下&#xff0c;选择打开或关闭Windows功能 3.等待加载&#xff0c;选择Internet信息服务&#xff0…

网站业务架构演变过程

有一天&#xff0c;我突发奇想创建了一个站点&#xff0c;基于LNMP架构&#xff0c;起初只有我自己访问&#xff0c;后来因为我点儿正&#xff0c;访问量越来越大&#xff0c;所以最终导致下面的架构演变。1. 单台机器因为只是一个小站&#xff0c;访问量一天也没有多少uv&…

做网站服务器e3,用e3做游戏服务器

用e3做游戏服务器 内容精选换一换下面以CentOS 6.9 (x86_64)操作系统为例&#xff0c;举例介绍裸金属服务器增强高速网卡的配置方法。RedHat系列、Oracle Linux系列、Euler系列及CentOS系列操作系统的配置方法类似。以“root”用户&#xff0c;使用密钥或密码登录裸金属服务器。…

Python+Flask+MysqL的web建设技术开发一个网站

一、摘要 flask是一个很精简&#xff0c;灵活的框架&#xff0c;对于web的开发非常的好&#xff0c;具有jinja2强大的模板引擎的支持。flask框架的一个扩展就是sqlalchemy, sqlalcheny是flask的一个扩展。sqlalcheny是一个强大的关系型数据库框架&#xff0c;它是一个框架&…

MCSE笔记 第四章 IIS网站架设

以前大学时候上过网络操作系统这门课程&#xff0c;当时用的是Windows 2003弄的IIS&#xff0c;工作中接触过2008、2003系统搭建的IIS&#xff0c;都不太熟悉&#xff0c;最近看的戴有炜老师的2012网络管理与架站&#xff0c;整理一下IIS章节的笔记。大致内容如上图所示:1.安装…

网站规划通识:原型图绘制的一些注意事项

本文来自网易云社区作者&#xff1a;林玮园 雷火游戏部 网站组基本概念&#xff1a; 一、什么叫原型图&#xff1f;用线条、图形描绘出产品/专题的框架&#xff0c;即为原型&#xff0c;也可称线框图。原型图的输出可根据质量&#xff0c;大致分为低保真、中保真、高保真原型图…

客户网站被挂马的分析

打开网站,查看 源代码 ,查找<iframe 标签 就会找到在这段框架:<iframe src"http://www.fengyajade.com/jiaozhu.htm" name"zhu" width"0" height"0" frameborder"0">这就是 打开网站为什么,杀毒软件提示有木马的原…

实现域名访问网站—nginx反向代理

今天在跟项目的时候&#xff0c;上午被耍了三个多小时&#xff0c;最后在我准备好材料准备他人求助的时候&#xff0c;在收集材料的时候&#xff0c;居然访问通了&#xff0c; 别问我为什么&#xff0c;我也不知道 &#xff0c;哈哈哈哈&#xff08;苦逼脸...&#xff09; 分享…

如何构建安全的电子商务网站

摘 要 本文总结了电子商务对安全性的要求&#xff0c;介绍了构成一个安全的电子商务系统的框架。介绍了安全通信协议SSL和SET&#xff0c;分析了其优缺点。最后&#xff0c;给出一个例子来指导如何建立一个基于SSL的安全网站。关键词 电子商务 PKI SSL TLS一、电子商务安…

企业网站制作之PageAdmin自助建站系统

企业网站现在已经是网络营销的标配&#xff0c;目前制作一个企业的网站&#xff0c;要么找网站制作公司&#xff0c;要么下载网站管理系统自己搭建&#xff0c;找网站公司的优点是&#xff1a;省心;缺点&#xff1a;后期维护和扩展都需要借助网站公司实现&#xff0c;完全依靠网…

[JavaScript]给自己的网站添加简单文本日志

1.用于记录日志的文件 log.asp&#xff0c;另外需要建立一个用于保存日志文件的文件夹logs。log.asp文件的代码如下&#xff1a; <script language"javascript"runat"server">varfso newActiveXObject("Scripting.FileSystemObject");vard…

视频网站上线测试了

要做视频网站的决心是在一次无意中的观看优酷视频得来的&#xff0c;因为当时学过nodejs的爬虫&#xff0c;所以对于优酷开放外链的行为感到高兴&#xff0c;因为这样子就代表我能够将优酷里面的大多数视频都爬下来&#xff0c;然后就可以做一个视频的网站了。就是这股劲&#…

局域网内的网站访问

在使用虚拟机的情况下&#xff0c;我在虚拟机上安装好了Apache后&#xff0c;httpd.conf里面也配置好了虚拟主机&#xff0c;如下&#xff1a; <VirtualHost 192.168.0.117>DocumentRoot "/var/www/html/"ServerName 192.168.0.117 #DirectoryIndex index…