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.     }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM