不过我发现农历网提供的查询到2100年农历其中闰月大小有些错误,表明闰月小的实际上有三十,而表明闰月大的却只有二十九,为何?
//农历年算法ASP。NET2.0-侯育胜依据网络找到资料参考编写,希望有助于人,若有错误处,请高手指点。1900-2100年。
string TianGan = "庚辛壬癸甲乙丙丁戊己", DiZhi = "申酉戌亥子丑寅卯辰巳午未", ShengXiao = "猴鸡狗猪鼠牛虎兔龙蛇马羊", strTime = System.DateTime.Now.ToString();
string Nl_Yue = "正二三四五六七八九十寒腊", Nl_Ri = "初一初二初三初四初五初六初七初八初九初十十一十二十三十四十五十六十七十八十九二十廿一廿二廿三廿四廿五廿六廿七廿八廿九三十";
string Nian = DateTime.Now.Year.ToString(), Yue = DateTime.Now.Month.ToString(), Ri = DateTime.Now.Day.ToString();
int Tg = Convert.ToInt16(Nian.Substring(Nian.Length - 1, 1));
int Dz = Convert.ToInt16(Nian) % 12;
//农历月算法
// 下面是一个关于C#的农历算法
// 日期数据定义方法如下
// 前12个字节代表1-12月为大月或是小月,1为大月30天,0为小月29天,闰月不计。
// 第13位为闰月的情况,1为大月30天,0为小月29天,第14、15位为闰月的月份,使用10进制表示,如果没有闰月则为000。
// 最后4位为当年家农历新年-即农历1月1日所在公历的日期,如0131代表1月31日。
string[] N_L_B = new string[] {
"0100101101101080131", "0100101011100000219", "1010010101110000208", "0101001001101050129", "1101001001100000216", "1101100101010000204",
"0110101010101040125", "0101011010100000213", "1001101011010000202", "0100101011101020122", "0100101011100000210", "1010010011011060130",
"1010010011010000218", "1101001001010000206", "1101010101001050126", "1011010101010000214", "0101011010100000204", "1001011011010020123",
"1001010110110000211", "0100100110111070201", "0100100110110000220", "1010010010110000208", "1011001001011050128", "0110101001010000216",
"0110110101000000205", "1010110110101040124", "0010101101100000213", "1001010101110000202", "0100100101111020123", "0100100101110000210",
"0110010010110060130", "1101010010100000217", "1110101001010000206", "0110110101001050126", "0101101011010000214", "0010101101100000204",
"1001001101110030124", "1001001011100000211", "1100100101101070131", "1100100101010000219", "1101010010100000208", "1101101001010060127",
"1011010101010000215", "0101011010100000205", "1010101011011040125", "0010010111010000213", "1001001011010000202", "1100100101011020122",
"1010100101010000210", "1011010010101070129", "0110110010100000217", "1011010101010000206", "0101010110101050127", "0100110110100000214",
"1010010110110000203", "0101001010111030124", "0101001010110000212", "1010100101010080131", "1110100101010000218", "0110101010100000208",
"1010110101010060128", "1010101101010000215", "0100101101100000205", "1010010101110040125", "1010010101110000213", "0101001001100000202",
"1110100100110030121", "1101100101010000209", "0101101010101070130", "0101011010100000217", "1001011011010000206", "0100101011101050127",
"0100101011010000215", "1010010011010000203", "1101001001101040123", "1101001001010000211", "1101010100101080131", "1011010101000000218",
"1011011010100000207", "1001011011010060128", "1001010110110000216", "0100100110110000205", "1010010010111040125", "1010010010110000213",
"1011001001011100202", "0110101001010000220", "0110110101000000209", "1010110110101060129", "1010101101100000217", "1001001101110000206",
"0100100101111050127", "0100100101110000215", "0110010010110000204", "0110101001010030123", "1110101001010000210", "0110101100101080131",
"0101101011000000219", "1010101101100000207", "1001001101101050128", "1001001011100000216", "1100100101100000205", "1101010010101040124",
"1101010010100000212", "1101101001010000201", "0101101010101020122", "0101011010100000209", "1010101011011070129", "0010010111010000218",
"1001001011010000207", "1100100101011050126", "1010100101010000214", "1011010010100000214", "1011010101010050123", "1010110101010000210",
"0101010111011100131", "0100101110100000219", "1010010110110000208", "0101000101111070128", "0101001010110000216", "1010100100010000205",
"0111100101001050125", "0110101010100000212", "1010110101010000201", "0101101101011030121", "0100101101100000210", "1010011101110070129",
"1010010011100000217", "1101001001100000206", "1110101001101060126", "1101010100110000213", "0101101010100000203", "0111011010101040123",
"1001011011010000211", "0100101011111120131", "0100101011010000219", "1010010011010000208", "1101000010110070127", "1101001001010000215",
"1101010100100000204", "1101110101001060124", "1011010110100000212", "0101011011010000201", "0101010110111030122", "0100100110110000210",
"1010010101110080130", "1010010010110000217", "1010101001010000206", "1011001001011060126", "0110110100100000214", "1010110110100000202",
"0100101101101040123", "1001001101110000211", "0100100111110090201", "0100100101110000219", "0110010010110000208", "0110100010101070128",
"1110101001010000215", "0110101100100000204", "1010011011001050124", "1010101011100000212", "1001001011100000202", "1101001011100040121",
"1100100101100000209", "1101010101010080129", "1101010010100000217", "1101101001010000205", "0101110101010060126", "0101011010100000214",
"1010011011010000203", "0101010111010050123", "0101001011010000211", "1010100110110090131", "1010100101010000219", "1011010010100000207",
"1011011010100070127", "1010110101010000215", "0101010110100000205", "1010101110100050124", "1010010110110000212", "0101001010110000202",
"1011001001110040122", "0110100100110000209", "0111001100110080129", "0110101010100000217", "1010110101010000206", "0100101101011060126",
"0100101101100000214", "1010010101110000203", "0101010011100050124", "1101001001100000210", "1110100101100090130", "1101010100100000218",
"1101101010100000207", "0110101010101070127", "0101011011010000215", "0100101011100000205", "1010100111010050125", "1010010011010000212",
"1101000101010000201", "1111001001010030121", "1101010100100000209", };
int N_Zd = 2100, N_Zx = 1900, N_L_N = 0, N_L_Y = 0, N_L_R = 0;// string Nian = Nian;
if (Convert.ToInt16(Nian) <= N_Zd || Convert.ToInt16(Nian) >= N_Zx)
{
N_L_Y = Convert.ToInt16(N_L_B[Convert.ToInt16(Nian) - N_Zx].Substring(15, 2));
N_L_R = Convert.ToInt16(N_L_B[Convert.ToInt16(Nian) - N_Zx].Substring(17, 2));
}
if ((Convert.ToInt16(Yue) < N_L_Y) || ((Convert.ToInt16(Yue) == N_L_Y) && (Convert.ToInt16(Ri) < N_L_R)))
{
N_L_N = Convert.ToInt16(Nian);
N_L_N--;
// 农历新年月份
N_L_Y = Convert.ToInt16((N_L_B[N_L_N - N_Zx].Substring(15, 2)));
// 农历新年日子
N_L_R = Convert.ToInt16((N_L_B[N_L_N - N_Zx].Substring(17, 2)));
}
else { N_L_N = Convert.ToInt16(Nian); }
// 计算农历月
DateTime GlDateTime = new DateTime(Convert.ToInt16(Nian), Convert.ToInt16(Yue), Convert.ToInt16(Ri));
DateTime NlDateTime = new DateTime(N_L_N, N_L_Y, N_L_R);
TimeSpan Ri_Qi_Ca = GlDateTime - NlDateTime;
int TianShu = Ri_Qi_Ca.Days;
N_L_Y = 1; N_L_R = 1;
bool Run_Yue = false;
for (int i = 0; i < TianShu; i++)
{
N_L_R++;
if (N_L_R == 30 + Convert.ToInt16(N_L_B[N_L_N - N_Zx].Substring(N_L_Y - 1, 1)) || (Run_Yue && (N_L_R == 30 + Convert.ToInt16(N_L_B[N_L_N - N_Zx].Substring(12, 1)))))
{
if ((Run_Yue == false) && (N_L_Y == Convert.ToInt16(N_L_B[N_L_N - N_Zx].Substring(13, 2))))
{ Run_Yue = true; }
else
{ Run_Yue = false; N_L_Y++; }
N_L_R = 1;
}
else
{ }
}
//转换计算农历日
string S_N_L_R = Nl_Ri.Substring((N_L_R - 1) * 2, 2);
//转换计算农历月
string S_N_L_Y = Nl_Yue.Substring(N_L_Y - 1, 1) + "月";
if (Run_Yue)
{ S_N_L_Y = "闰" + N_L_Y; }
//转换农历年string S_N_L_N = Convert.ToString(N_L_N, 10) + "年";
//输出农历日期总合
Label1.Text += TianGan.Substring(Tg, 1) + DiZhi.Substring(Dz, 1) + "年【" + ShengXiao.Substring(Dz, 1) + "】" + S_N_L_Y + S_N_L_R + "日";