C# 将系统时间转换成农历时间
直接上代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace RSMISClient 6 { 7 using System.Globalization; 8 public static class ChineseDate 9 { 10 private static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar(); 11 //cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28 12 //cCalendar.MinSupportedDateTime 返回支持的最小日期,即1901-2-19 13 14 #region 农历年 15 /// <summary> 16 /// 十天干 17 /// </summary> 18 private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; 19 20 /// <summary> 21 /// 十二地支 22 /// </summary> 23 private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; 24 25 /// <summary> 26 /// 十二生肖 27 /// </summary> 28 private static string[] shengxiao = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; 29 30 /// <summary> 31 /// 返回农历天干地支年 32 /// </summary> 33 /// <param name="year">农历年</param> 34 /// <returns></returns> 35 public static string GetLunisolarYear(int year) 36 { 37 if (year > 3) 38 { 39 int tgIndex = (year - 4) % 10; 40 int dzIndex = (year - 4) % 12; 41 42 return string.Concat(tiangan[tgIndex], dizhi[dzIndex], "[", shengxiao[dzIndex], "]"); 43 } 44 45 throw new ArgumentOutOfRangeException("无效的年份!"); 46 } 47 #endregion 48 49 #region 农历月 50 /// <summary> 51 /// 农历月 52 /// </summary> 53 private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一(冬)", "十二(腊)" }; 54 55 /// <summary> 56 /// 返回农历月 57 /// </summary> 58 /// <param name="month">月份</param> 59 /// <returns></returns> 60 public static string GetLunisolarMonth(int month) 61 { 62 if (month < 13 && month > 0) 63 { 64 return months[month - 1]; 65 } 66 67 throw new ArgumentOutOfRangeException("无效的月份!"); 68 } 69 #endregion 70 71 #region 农历日 72 /// <summary> 73 /// 74 /// </summary> 75 private static string[] days1 = { "初", "十", "廿", "三" }; 76 77 /// <summary> 78 /// 日 79 /// </summary> 80 private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" }; 81 82 /// <summary> 83 /// 返回农历日 84 /// </summary> 85 /// <param name="day"></param> 86 /// <returns></returns> 87 public static string GetLunisolarDay(int day) 88 { 89 if (day > 0 && day < 32) 90 { 91 if (day != 20 && day != 30) 92 { 93 return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]); 94 } 95 else 96 { 97 return string.Concat(days[(day - 1) / 10], days1[1]); 98 } 99 } 100 101 throw new ArgumentOutOfRangeException("无效的日!"); 102 } 103 #endregion 104 105 #region 方法 106 public static string GetChineseDateTime(DateTime datetime) 107 { 108 int lyear = cCalendar.GetYear(datetime); 109 int lmonth = cCalendar.GetMonth(datetime); 110 int lday = cCalendar.GetDayOfMonth(datetime); 111 112 //获取闰月, 0 则表示没有闰月 113 int leapMonth = cCalendar.GetLeapMonth(lyear); 114 bool isleap = false; 115 116 if (leapMonth > 0) 117 { 118 if (leapMonth == lmonth) 119 { 120 //闰月 121 isleap = true; 122 lmonth--; 123 } 124 else if (lmonth > leapMonth) 125 { 126 lmonth--; 127 } 128 } 129 130 return string.Concat(GetLunisolarYear(lyear), "年 ", isleap ? "闰" : string.Empty, GetLunisolarMonth(lmonth), "月·", GetLunisolarDay(lday)); 131 } 132 #endregion 133 } 134 135 }