C#项目获取当前时间的农历时间
https://blog.csdn.net/cstester/article/details/7407044
- using System.Globalization;
- class CnCanlendar_nongli
- {
- /// <summary>
- /// 实例化一个 ChineseLunisolarCalendar
- /// </summary>
- private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
- /// <summary>
- /// 十天干
- /// </summary>
- private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
- /// <summary>
- /// 十二地支
- /// </summary>
- private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
- /// <summary>
- /// 十二生肖
- /// </summary>
- private static string[] sx = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
- /// <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(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
- }
- throw new ArgumentOutOfRangeException("无效的年份!");
- }
- /// <summary>
- /// 农历月
- /// </summary>
- /// <returns></returns>
- private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };
- /// <summary>
- /// 农历日
- /// </summary>
- private static string[] days1 = { "初", "十", "廿", "三" };
- /// <summary>
- /// 农历日
- /// </summary>
- private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
- /// <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("无效的月份!");
- }
- /// <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("无效的日!");
- }
- /// <summary>
- /// 根据公历获取农历日期
- /// </summary>
- /// <param name="datetime">公历日期</param>
- /// <returns></returns>
- public static string GetChineseDateTime(DateTime datetime)
- {
- //农历的年月日
- int year = ChineseCalendar.GetYear(datetime);
- int month = ChineseCalendar.GetMonth(datetime);
- int day = ChineseCalendar.GetDayOfMonth(datetime);
- //获取闰月, 0 则表示没有闰月
- int leapMonth = ChineseCalendar.GetLeapMonth(year);
- bool isleap = false;
- if (leapMonth > 0)
- {
- if (leapMonth == month)
- {
- //闰月
- isleap = true;
- month--;
- }
- else if (month > leapMonth)
- {
- month--;
- }
- }
- return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
- }
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!