博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net 如何判断农历节日
阅读量:4212 次
发布时间:2019-05-26

本文共 3014 字,大约阅读时间需要 10 分钟。

http://ilewen.com/questions/3480

如下:

使用

static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28cCalendar.MinSupportedDateTime  返回支持的最小日期,即1901-2-19

下面我们来实现公历转农历。

/// /// 根据公历获取农历日期/// /// 公历日期/// 
public static string GetChineseDateTime(DateTime datetime){
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--; } } return string.Concat(GetLunisolarYear(datetime.Year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday));}

测试的结果:

传入日期:2010-3-4

返回农历:庚寅[虎]年正月十九

可以满足简单的需求啦。

其他代码也附上:

region 农历年

/// /// 十天干/// private static string[] tiangan = {
"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };/// /// 十二地支/// private static string[] dizhi = {
"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };/// /// 十二生肖/// private static string[] shengxiao = {
"鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };/// /// 返回农历天干地支年 /// /// 公历年///
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 农历月

/// /// 农历月/// private static string[] months = {
"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };/// /// 返回农历月/// /// 月份///
public static string GetLunisolarMonth(int month){
if (month < 13 && month > 0) {
return months[month - 1]; } throw new ArgumentOutOfRangeException("无效的月份!");}#endregion

region 农历日

/// /// /// private static string[] days1 = {
"初", "十", "廿", "三" };/// /// 日/// private static string[] days = {
"一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };/// /// 返回农历日/// /// ///
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 / 10], days1[1]); } } throw new ArgumentOutOfRangeException("无效的日!");}#endregion

还有一个根据日期获取生肖的代码:

/// /// 返回生肖/// /// 公历日期/// 
public static string GetShengXiao(DateTime datetime){
return shengxiao[cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(datetime)) - 1];}
 
   

转载地址:http://dpzmi.baihongyu.com/

你可能感兴趣的文章
C/C++文件操作[转载]
查看>>
专业计划
查看>>
小米笔试:最大子数组乘积
查看>>
常见的排序算法
查看>>
5.PyTorch实现逻辑回归(二分类)
查看>>
6.PyTorch实现逻辑回归(多分类)
查看>>
8.Pytorch实现5层全连接结构的MNIST(手写数字识别)
查看>>
9.PyTorch实现MNIST(手写数字识别)(2卷积1全连接)
查看>>
hdu 3460 Ancient Printer(trie tree)
查看>>
中间数
查看>>
KMP求前缀函数(next数组)
查看>>
KMP
查看>>
poj 3863Business Center
查看>>
Android编译系统简要介绍和学习计划
查看>>
Android编译系统环境初始化过程分析
查看>>
user2eng 笔记
查看>>
DRM in Android
查看>>
ARC MRC 变换
查看>>
Swift cell的自适应高度
查看>>
【linux】.fuse_hiddenXXXX 文件是如何生成的?
查看>>