直接上码,都有注释说明

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;namespace ConsoleApp.Models
{public class ChineseCalendarModel{/// <summary>/// 构造函数   CreateDate:2020-01-09 17:45:57;Author:Ling_bug/// </summary>/// <param name="date"></param>public ChineseCalendarModel(DateTime date){Init(date);}/// <summary>/// 初始化   CreateDate:2020-01-09 17:46:28;Author:Ling_bug/// </summary>/// <param name="date"></param>private void Init(DateTime date){this.ParameterDate = date;char o = '、';//十天干string a = "甲、乙、丙、丁、戊、己、庚、辛、壬、癸";this.HeavenlyStems = a.Split(o);//十二地支string b = "子、丑、寅、卯、辰、巳、午、未、申、酉、戌、亥";this.EarthlyBranches = b.Split(o);//十二生肖string c = "鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪";this.ChineseZodiacs = c.Split(o);//月份string d = "正、二、三、四、五、六、七、八、九、十、十一、腊";this.Months = d.Split(o);//日期开头string e = "初、十、甘、三";this.DaysTitle = e.Split(o);//日期结尾string f = "一、二、三、四、五、六、七、八、九、十";this.Days = f.Split(o);InitConstellations();InitConstellationData();//获取农历日期InitChineseCalendarDate();}/// <summary>/// 找到参数对应的星座   CreateDate:2020-01-10 11:03:49;Author:Ling_bug/// </summary>private void InitConstellationData(){int month = this.ParameterDate.Month;int day = this.ParameterDate.Day;//找到与开始日期匹配的或者结束日期匹配的(说明该参数日期是这个星座的开始的那一天或者结束的那一天)var equalModel = this.Constellations.FirstOrDefault(r => (r.BeginMonth == month && r.BeginDay == day) || (r.EndMonth == month && r.EndDay == day));if (equalModel == null){bool isFindFromEnd = true;//找到开始月相同的var beginMonthModel = this.Constellations.FirstOrDefault(r => r.BeginMonth == month);//如果找到了并且当前日大于开始日,则匹配正确if (beginMonthModel != null && day > beginMonthModel.BeginDay){this.ConstellationName = beginMonthModel.ConstellationName;isFindFromEnd = false;}//如果没有找到,则匹配结束的if (isFindFromEnd){//找到结束月相同的var endMonthModel = this.Constellations.FirstOrDefault(r => r.EndMonth == month);//如果有结束月相同的,并且当前日小于结束日的,则匹配成功if (endMonthModel != null && day < endMonthModel.EndDay){this.ConstellationName = endMonthModel.ConstellationName;}}}else{this.ConstellationName = equalModel.ConstellationName;}}/// <summary>/// 初始化星座集合   CreateDate:2020-01-10 11:03:23;Author:Ling_bug/// </summary>private void InitConstellations(){this.Constellations = new List<ConstellationModel>(){new ConstellationModel("白羊", "3-21", "4-19"),new ConstellationModel("金牛", "4-20", "5-20"),new ConstellationModel("双子", "5-21", "6-21"),new ConstellationModel("巨蟹", "6-22", "7-22"),new ConstellationModel("狮子", "7-23", "8-22"),new ConstellationModel("处女", "8-23", "9-22"),new ConstellationModel("天秤", "9-23", "10-23"),new ConstellationModel("天蝎", "10-24", "11-22"),new ConstellationModel("射手", "11-23", "12-21"),new ConstellationModel("摩羯", "12-22", "1-19"),new ConstellationModel("水瓶", "1-20", "2-18"),new ConstellationModel("双鱼", "2-19", "3-20")};}/// <summary>/// 获取到农历日期   CreateDate:2020-01-09 17:46:52;Author:Ling_bug/// </summary>private void InitChineseCalendarDate(){//农历日期服务类var calendarService = new ChineseLunisolarCalendar();//农历年this.Year = calendarService.GetYear(this.ParameterDate);//农历月(闰年时不同)this.Month = calendarService.GetMonth(this.ParameterDate);//农历日this.Day = calendarService.GetDayOfMonth(this.ParameterDate);//获取到该年的闰月this.LeapMonth = calendarService.GetLeapMonth(this.Year);this.InitResultMonth();this.InitLunisolarTitle();//农历日期字符串this.ChineseCalendarDateStr = $"{this.HeavenlyStem}{this.EarthlyBranch}{this.ChineseZodiac}年{this.IsLeapTitle}{this.MonthTitle}月{this.DayTitle}[{this.Year}-{this.ResultMonth}-{this.Day}]";}/// <summary>/// 获取到真正的农历月(考虑闰月的情况)   CreateDate:2020-01-09 17:48:31;Author:Ling_bug/// </summary>private void InitResultMonth(){this.ResultMonth = this.Month;if (this.LeapMonth > 0){this.IsLeap = this.Month == this.LeapMonth;//如果当前月大于等于闰月,则需要减一(例如闰月是7月,查询出来是8月,实际上当前月份其实是7月,所以需要减一)if (this.Month >= this.LeapMonth){this.ResultMonth--;}}this.IsLeapTitle = this.IsLeap ? "闰" : string.Empty;}/// <summary>/// 获取到要显示的title   CreateDate:2020-01-09 18:23:35;Author:Ling_bug/// </summary>private void InitLunisolarTitle(){var yearHelper = this.Year - 4;var indexHeavenlyStem = yearHelper % 10;var indexEarthlyBranchAndChineseZodiac = yearHelper % 12;this.HeavenlyStem = this.HeavenlyStems[indexHeavenlyStem];this.EarthlyBranch = this.EarthlyBranches[indexEarthlyBranchAndChineseZodiac];this.ChineseZodiac = this.ChineseZodiacs[indexEarthlyBranchAndChineseZodiac];this.MonthTitle = this.Months[this.ResultMonth - 1];InitDayTitle();}/// <summary>/// 获取到年月日中日的title   CreateDate:2020-01-09 18:24:13;Author:Ling_bug/// </summary>private void InitDayTitle(){var dayHelper = this.Day - 1;var indexHelperDayDiv = dayHelper / 10;if (this.Day == 20 || this.Day == 30){this.DayTitle = this.Days[indexHelperDayDiv] + this.DaysTitle[1];}else{var indexHelperDayMod = dayHelper % 10;this.DayTitle = this.DaysTitle[indexHelperDayDiv] + this.Days[indexHelperDayMod];}}/// <summary>/// 需要转换的公历日期参数/// </summary>public DateTime ParameterDate { get; set; }/// <summary>/// 农历年/// </summary>public int Year { get; set; }/// <summary>/// 农历月(闰月时不同)/// </summary>public int Month { get; set; }/// <summary>/// 农历月(自动计算考虑闰月的情况)/// </summary>public int ResultMonth { get; set; }/// <summary>/// 农历日/// </summary>public int Day { get; set; }/// <summary>/// 今年哪个月是闰月/// </summary>public int LeapMonth { get; set; }/// <summary>/// 当前是否是闰月/// </summary>public bool IsLeap { get; set; }/// <summary>/// 十天干/// </summary>public string[] HeavenlyStems { get; private set; }/// <summary>/// 十二地支/// </summary>public string[] EarthlyBranches { get; private set; }/// <summary>/// 十二生肖/// </summary>public string[] ChineseZodiacs { get; private set; }/// <summary>/// 星座/// </summary>public List<ConstellationModel> Constellations { get; private set; }/// <summary>/// 月/// </summary>public string[] Months { get; private set; }/// <summary>/// 日期头/// </summary>public string[] DaysTitle { get; private set; }/// <summary>/// 日期尾/// </summary>public string[] Days { get; private set; }/// <summary>/// 天干/// </summary>public string HeavenlyStem { get; set; }/// <summary>/// 地支/// </summary>public string EarthlyBranch { get; set; }/// <summary>/// 生肖/// </summary>public string ChineseZodiac { get; set; }/// <summary>/// 星座/// </summary>public string ConstellationName { get; set; }/// <summary>/// 月/// </summary>public string MonthTitle { get; set; }/// <summary>/// 日期/// </summary>public string DayTitle { get; set; }/// <summary>/// 闰月/// </summary>public string IsLeapTitle { get; set; }/// <summary>/// 结果字符串/// </summary>public string ChineseCalendarDateStr { get; set; }}
}

匹配星座还有一种写法:

            var constellationList = this.Constellations.FindAll(r =>{bool begin = r.BeginMonth == month && r.BeginDay == day;bool end = r.EndMonth == month && r.EndDay == day;bool granterBegin = r.BeginMonth == month && day > r.BeginDay;bool lessEnd = r.EndMonth == month && day < r.EndDay;return begin || end || granterBegin || lessEnd;});if (constellationList.Any()){if (constellationList.Count == 1){this.ConstellationName = constellationList.First().ConstellationName;}else{throw new Exception($"匹配到{constellationList.Count}个星座,month = {month},day = {day}");}}else{throw new Exception($"未匹配到星座,month = {month},day = {day}");}

星座对象:

using System;namespace ConsoleApp.Models
{/// <summary>/// 星座   CreateDate:2020-01-10 10:19:23;Author:Ling_bug/// </summary>public class ConstellationModel{public ConstellationModel(string constellationName, string beginDate, string endDate){this.ConstellationName = constellationName;if (this.ConstellationName.Length == 2){this.ConstellationName += "座";}this.BeginDate = Convert.ToDateTime(beginDate);this.EndDate = Convert.ToDateTime(endDate);this.BeginMonth = this.BeginDate.Month;this.BeginDay = this.BeginDate.Day;this.EndMonth = this.EndDate.Month;this.EndDay = this.EndDate.Day;}public DateTime BeginDate { get; private set; }public int BeginMonth { get; private set; }public int BeginDay { get; private set; }public int EndMonth { get; private set; }public int EndDay { get; private set; }public DateTime EndDate { get; private set; }public string ConstellationName { get; private set; }}
}

测试一下:

测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using ConsoleApp.Models;namespace ConsoleApp.Services
{public class TestChineseCalendarService{public static void Run1(){var model = new ChineseCalendarModel(DateTime.Now);int index = 0;foreach (var proc in model.GetType().GetProperties()){var value = proc.GetValue(model);Console.WriteLine($"{++index}.{proc.Name}:{value}");}}public static void Run2(){var begin = Convert.ToDateTime("2020-1-1");var end = Convert.ToDateTime("2020-12-31");int count = 0;var list = new List<ChineseCalendarModel>();while (begin <= end){var item = new ChineseCalendarModel(begin);list.Add(item);Console.WriteLine(begin.ToString("yyyy-MM-dd") + ":" + item.ChineseCalendarDateStr + $"[{item.ConstellationName}]");begin = begin.AddDays(1);count++;}Console.WriteLine();Console.WriteLine($"共{count}条");Console.WriteLine($"未匹配到星座的:{list.Count(r => string.IsNullOrWhiteSpace(r.ConstellationName))}");}}
}

Run1测试结果:

Run2测试结果:

Ending~

根据公历日期获取到农历日期信息(带星座)(C#)相关推荐

  1. html里获得农历时间,用JavaScript获取当前农历日期

    用JavaScript获取当前农历日期_网页代码站(www.webdm.cn) Welcome Materials System var sWeek = new Array("星期日&quo ...

  2. 根据当期日期计算,农历日期的类

    根据当期日期计算,农历日期的类 public class Lunar {private int year; private int month; private int day; private bo ...

  3. Vue element 日期获取展示今天日期

    element 日期获取当前日期 <el-date-pickerv-model="formData.startTime"type="date"placeh ...

  4. 根据日期获取生肖,根据日期获取星座

    /*** 根据日期获取生肖* @return*/public static String getZodica(Date date) {public String[] zodiacArr = { &qu ...

  5. Python:zhdate模块农历日期处理

    简介:zhdate模块统计从1900年到2100年的农历月份数据代码,支持农历和公历之间的转化,并且支持日期差额运算. 安装: pip install zhdate 主要功能: 1.获取公历对应的农历 ...

  6. Java 获取两个日期之间的日期

    1.前期需求,两个日期,startDate和endDate,然后获取到两个日期之间的日期 /*** 获取两个日期之间的日期* @param start 开始日期* @param end 结束日期* @ ...

  7. server sql 时间差分钟_SqlServer获取两个日期时间差

    SELECT datediff(yy,'2010-06-1 10:10',GETDATE()) --计算多少年 SELECT datediff(q,'2011-01-1 10:10',GETDATE( ...

  8. mysql 获取农历年份_iOS 获取公历、农历日期的年月日

    iOS 获取公历.农历日期的年月日 介绍三种方法获取 Date (NSDate) 的年月日. 用 date 表示当前日期.测试日期为公历 2017 年 2 月 5 日,农历丁酉年,鸡年,正月初九. l ...

  9. java 获取阴历日期公历日期转农历日期或者阳历日期转阴历日期

    背景 项目中需要获取农历日期,展示在页面,不使用第三方工具类的情况下,直接使用原生的java api工具类开发 代码如下 package com.hidata.devops.paas;import j ...

最新文章

  1. 专访 | 社科学院和美术学院毕业生与大数据的故事【第一届数据故事计划】
  2. matlab中fdyn,Matlab的用法总结
  3. python连接mongo_Python连接MongoDB操作
  4. python编程和c语言编程的区别-C语言 python Java 等主要流行编程语言优劣对比
  5. 广东春运安保工作提前10天启动
  6. @有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不...
  7. Access Token 与 Refresh Token【转载哒科普啊】
  8. [深度学习] Keras 如何使用fit和fit_generator
  9. ZooKeeper的安装和API
  10. 9.20 模拟试题
  11. 理解lua中的metatable和__index
  12. 数字化工厂的五大系统_工厂通过MES系统对车间设备数字化管理
  13. Atitit 区块链之道 attilax著 艾龙著 1. 金融=制度+技术+信息 1 2. 第一章可信的协议 1 3. 第二章引导未来:区块链经济七大设计原则 1 4. 第五章 新商业
  14. AI智能电话机器人源码搭建的原理
  15. C语言EasyX详解(小球碰撞)
  16. 【操作系统真象还原】Mac安装配置bochs
  17. 【历史上的今天】9 月 4 日:谷歌(Google) 23 周年;“人工智能之父” McCarthy 诞生
  18. 独家深挖!F1赛车协会“刹车表现”是如何进行数据分析的?
  19. float型数据表示的最大数字到底是多少(答案:0x7f7f ffff)
  20. hdwiki 数据库结构说明

热门文章

  1. 对于有关东方的题目的整理。。
  2. eclipse将项目打成war包步骤
  3. 跟着BOY学习开发cocos2d-x 游戏 实战篇(6)之 游戏主界面 -----绚丽的魔法特效
  4. Python 中常见的魔法方法
  5. 一直想找个老师学习Photoshop,今天终于找到了!!!
  6. 模拟CMOS 基础知识2——偏置与跨导
  7. 备战天华杯第一天(成长小日记)
  8. 外部设备如何连接到虚拟机中
  9. MPLS BGP 跨域方案A
  10. Frist Blog