C#项目获取当前时间的农历时间

https://blog.csdn.net/cstester/article/details/7407044

  1. using System.Globalization;  
  2.     class CnCanlendar_nongli  
  3.     {  
  4.         /// <summary>   
  5.         /// 实例化一个  ChineseLunisolarCalendar       
  6.         /// </summary>  
  7.         private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();  
  8.         /// <summary>  
  9.         /// 十天干  
  10.         /// </summary>  
  11.         private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };  
  12.         /// <summary>  
  13.         ///  十二地支  
  14.         ///  </summary>  
  15.         private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };  
  16.         /// <summary>  
  17.         /// 十二生肖  
  18.         /// </summary>  
  19.         private static string[] sx = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };  
  20.         /// <summary>  
  21.         ///  返回农历天干地支年  
  22.         ///   </summary>  
  23.         ///    <param name="year">农历年</param>  
  24.         ///    <returns></returns>  
  25.         public static string GetLunisolarYear(int year)  
  26.         {  
  27.             if (year > 3)  
  28.             {  
  29.                 int tgIndex = (year - 4) % 10;  
  30.                 int dzIndex = (year - 4) % 12;  
  31.                 return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");  
  32.             }  
  33.             throw new ArgumentOutOfRangeException("无效的年份!");  
  34.         }  
  35.         /// <summary>  
  36.         /// 农历月  
  37.         /// </summary>  
  38.         /// <returns></returns>  
  39.         private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };  
  40.         /// <summary>  
  41.         /// 农历日  
  42.         /// </summary>  
  43.         private static string[] days1 = { "初", "十", "廿", "三" };  
  44.         /// <summary>  
  45.         ///  农历日  
  46.         /// </summary>  
  47.         private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };  
  48.         /// <summary>  
  49.         /// 返回农历月  
  50.         /// </summary>  
  51.         /// <param name="month">月份</param>  
  52.         /// <returns></returns>  
  53.         public static string GetLunisolarMonth(int month)  
  54.         {  
  55.             if (month < 13 && month > 0)  
  56.             {  
  57.                 return months[month - 1];  
  58.             }  
  59.             throw new ArgumentOutOfRangeException("无效的月份!");  
  60.         }  
  61.         /// <summary>  
  62.         /// 返回农历日  
  63.         /// </summary>  
  64.         /// <param name="day">天</param>  
  65.         /// <returns></returns>  
  66.         public static string GetLunisolarDay(int day)  
  67.         {  
  68.             if (day > 0 && day < 32)  
  69.             {  
  70.                 if (day != 20 && day != 30)  
  71.                 {  
  72.                     return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);  
  73.                 }  
  74.                 else  
  75.                 {  
  76.                     return string.Concat(days[(day - 1) / 10], days1[1]);  
  77.                 }  
  78.             }  
  79.             throw new ArgumentOutOfRangeException("无效的日!");  
  80.         }  
  81.   
  82.         /// <summary>   
  83.         /// 根据公历获取农历日期  
  84.         /// </summary>  
  85.         /// <param name="datetime">公历日期</param>  
  86.         /// <returns></returns>  
  87.         public static string GetChineseDateTime(DateTime datetime)  
  88.         {  
  89.             //农历的年月日  
  90.             int year = ChineseCalendar.GetYear(datetime);  
  91.             int month = ChineseCalendar.GetMonth(datetime);  
  92.             int day = ChineseCalendar.GetDayOfMonth(datetime);  
  93.             //获取闰月, 0 则表示没有闰月   
  94.             int leapMonth = ChineseCalendar.GetLeapMonth(year);  
  95.             bool isleap = false;  
  96.             if (leapMonth > 0)  
  97.             {  
  98.                 if (leapMonth == month)  
  99.                 {  
  100.                     //闰月       
  101.                     isleap = true;  
  102.                     month--;  
  103.                 }  
  104.                 else if (month > leapMonth)  
  105.                 {  
  106.                     month--;  
  107.                 }  
  108.             }  
  109.             return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));  
  110.         }  
  111.     }  
原文地址:https://www.cnblogs.com/zkwarrior/p/9030268.html