C#公历转农历算法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

///



002 /// LunDay 的摘要说明。

003 /// 用法说明

004 /// 直接调用即可,比较简单

005 ///


006 public class LunDay

007 {

008 public LunDay()

009 {

010 //

011 // TODO: 在此处添加构造函数逻辑

012 //

013 }

014 //天干

015 private static string[] TianGan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

016

017 //地支

018 private static string[] DiZhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };

019

020 //十二生肖

021 private static string[] ShengXiao = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

022

023 //农历日期

024 private static string[] DayName = {"*","初一","初二","初三","初四","初五",

025 "初六","初七","初八","初九","初十",

026 "十一","十二","十三","十四","十五",

027 "十六","十七","十八","十九","二十",

028 "廿一","廿二","廿三","廿四","廿五",

029 "廿六","廿七","廿八","廿九","三十"};

030

031 //农历月份

032 private static string[] MonthName = { "*", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊" };

033

034 //公历月计数天

035 private static int[] MonthAdd = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };

036 //农历数据

037 private static int[] LunarData = {2635,333387,1701,1748,267701,694,2391,133423,1175,396438

038 ,3402,3749,331177,1453,694,201326,2350,465197,3221,3402

039 ,400202,2901,1386,267611,605,2349,137515,2709,464533,1738

040 ,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762

041 ,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413

042 ,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395

043 ,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031

044 ,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222

045 ,268949,3402,3493,133973,1386,464219,605,2349,334123,2709

046 ,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877};

047 ///

048 /// 获取对应日期的农历

049 ///


050 /// 公历日期

051 ///

052 public string GetLunarCalendar(DateTime dtDay)

053 {

054 string sYear = dtDay.Year.ToString();

055 string sMonth = dtDay.

Month.ToString();

056 string sDay = dtDay.Day.ToString();

057 int year;

058 int month;

059 int day;

060 try

061 {

062 year = int.Parse(sYear);

063 month = int.Parse(sMonth);

064 day = int.Parse(sDay);

065 }

066 catch

067 {

068 year = DateTime.Now.Year;

069 month = DateTime.Now.Month;

070 day = DateTime.Now.Day;

071 }

072

073 int nTheDate;

074 int nIsEnd;

075 int k, m, n, nBit, i;

076 string calendar = string.Empty;

077 //计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)

078 nTheDate = (year - 1921) * 365 + (year - 1921) / 4 + day + MonthAdd[month - 1] - 38;

079 if ((year % 4 == 0) && (month > 2))

080 nTheDate += 1;

081 //计算天干,地支,月,日

082 nIsEnd = 0;

083 m = 0;

084 k = 0;

085 n = 0;

086 while (nIsEnd != 1)

087 {

088 if (LunarData[m] < 4095)

089 k = 11;

090 else

091 k = 12;

092 n = k;

093 while (n >= 0)

094 {

095 //获取LunarData[m]的第n个二进制位的值

096 nBit = LunarData[m];

097 for (i = 1; i < n + 1; i++)

098 nBit = nBit / 2;

099 nBit = nBit % 2;

100 if (nTheDate <= (29 + nBit))

101 {

102 nIsEnd = 1;

103 break;

104 }

105 nTheDate = nTheDate - 29 - nBit;

106 n = n - 1;

107 }

108 if (nIsEnd == 1)

109 break;

110 m = m + 1;

111 }

112 year = 1921 + m;

113 month = k - n + 1;

114 day = nTheDate;

115 //return year+"-"+month+"-"+day;

116

117 #region 格式化日期显示为三月廿四

118 if (k == 12)

119 {

120 if (month == LunarData[m] / 65536 + 1)

121 month = 1 - month;

122 else if (month > LunarData[m] / 65536 + 1)

123 month = month - 1;

124 }

125

126 //生肖

127 calendar = ShengXiao[(year - 4) % 60 % 12].ToString() + "

年 ";

128 //天干

129 calendar += TianGan[(year - 4) % 60 % 10].ToString();

130 //地支

131 calendar += DiZhi[(year - 4) % 60 % 12].ToString() + " ";

132

133 //农历月

134 if (month < 1)

135 calendar += "闰" + MonthName[-1 * month].ToString() + "月";

136 else

137 calendar += MonthName[month].ToString() + "月";

138

139 //农历日

140 calendar += DayName[day].ToString() + "日";

141

142 return calendar;

143

144 #endregion

145 }

146 }


相关文档
最新文档