关于C#公历转农历问题
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WinForm
{
class ChineseDate
{
private string[] m_strNongLiRiLiBiao;//农历日历表
private string[] m_strTianGanDiZhiBiao;//天干地支表
private string m_strShuXiangBiao;//属相表
private string m_strNongLiYueBiao;//农历月表
private string m_strNongLiRiBiao;//农历日表
private int m_iMaxYear;//最大公历年份
private int m_iMixYear;//最小公历年份
public ChineseDate()
{
#region 农历日历表
m_strNongLiRiLiBiao = new string[] {
"0100101101101080131",//1900
"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",
"1100101010110050126",//2009
"1010100101010000214",//2010
"1011010010100000203",//2011
"1011101010100040123",//2012
"1010110101010000210"//2013
};
#endregion
m_strShuXiangBiao = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
m_strNongLiYueBiao = "正二三四五六七八九十寒腊";
m_strNongLiRiBiao = "初一初二初三初四初五初六初七初八初九初十十一十二十三十四十五十六十七十八十九二十廿一廿二廿三廿四廿五廿六廿七廿八廿九三十";
m_iMaxYear = 2014;
m_iMixYear = 1900;
string str天干 = "甲乙丙丁戊已庚辛壬癸";
string str地支 = "子丑寅卯辰巳午未申酉戌亥";
m_strTianGanDiZhiBiao = new string[60];
for (int i = 0; i < 60; i++)
{
m_strTianGanDiZhiBiao[i] = str天干.Substring(i % 10, 1) + str地支.Substring(i % 12, 1);//天干地支查询
}
}
public string GetDate(int i公历年, int i公历月, int i公历日)
{
if ((i公历年 < m_iMixYear)
[解决办法]
(i公历年 > m_iMaxYear))
{ //如果不是有效公历日期,退出。
return "无效公历年份";
}
#region 计算农历年
int i农历年;
int i农历月;
int i农历日;
i农历年 = i公历年;
// 根据公历年来计算出当年农历新年月份(春节的公历月份)
i农历月 = Convert.ToInt32((m_strNongLiRiLiBiao[i农历年 - m_iMixYear].Substring(15, 2)));//这里截取第(i农历年 - m_iMixYear)个字符串,从15开始截取2个字符
// 计算出农历新年日子
i农历日 = Convert.ToInt32((m_strNongLiRiLiBiao[i农历年 - m_iMixYear].Substring(17, 2)));//同上,从17开始
if ((i公历月 < i农历月)
[解决办法]
((i公历月 == i农历月) && (i公历日 < i农历日)))//特殊情况,当农历月大于等于公历月时说明公历农历不是同一年,农历年需要减一
{
i农历年--;
// 农历新年月份
i农历月 = Convert.ToInt32((m_strNongLiRiLiBiao[i农历年 - m_iMixYear].Substring(15, 2)));
// 农历新年日子
i农历日 = Convert.ToInt32((m_strNongLiRiLiBiao[i农历年 - m_iMixYear].Substring(17, 2)));
}
#endregion
#region 计算农历月
DateTime dt公历日期 = new DateTime(i公历年, i公历月, i公历日);
DateTime dt农历日期 = new DateTime(i农历年, i农历月, i农历日);
TimeSpan ts日期差 = dt公历日期 - dt农历日期;
int i天数 = ts日期差.Days;//计算出所给日期与春节的日期差
i农历月 = 1;
i农历日 = 1;
bool b闰月 = false;
for (int i = 0; i < i天数; i++)
{
i农历日++;
if (i农历日 == 30 + Convert.ToInt32(m_strNongLiRiLiBiao[i农历年 - m_iMixYear].Substring(i农历月 - 1, 1))
[解决办法]
(b闰月 && (i农历日 == 30 + Convert.ToInt32(m_strNongLiRiLiBiao[i农历年 - m_iMixYear].Substring(12, 1)))))//以该月月底的日期来判断是不是*有可能*是闰月的前一个月
{
if ((b闰月 == false) && (i农历月 == Convert.ToInt32(m_strNongLiRiLiBiao[i农历年 - m_iMixYear].Substring(13, 2))))//判断下个月是不是闰月
{
b闰月 = true;
}
else
{
b闰月 = false;
i农历月++;
}
i农历日 = 1;
}
else { }
}
#endregion
// 计算农历日
string str农历日 = m_strNongLiRiBiao.Substring((i农历日 - 1) * 2, 2);
// 计算农历月
string str农历月 = m_strNongLiYueBiao.Substring(i农历月 - 1, 1) + "月";
if (b闰月)
{
str农历月 = "闰" + str农历月;
}
// 农历年
string str农历年 = Convert.ToString(i农历年, 10) + "年";
// 计算天干地支
string str天干地支 = m_strTianGanDiZhiBiao[(i农历年 - 4) % 60];
// 计算属相
string str属相 = m_strShuXiangBiao.Substring((i农历年 - 4) % 12, 1);
// 返回农历日期
return str属相 + "," + str天干地支 + "," + str农历年 + str农历月 + str农历日;
}
}
}
//////////////////////////////////////////////////////////////////////////
//下面分析一下数组中各数位代表的含义,以"1010110101010000210"//2013为例
//
//先数下101011010101 0 00 0210的位数为19位
//从语句m_strNongLiRiLiBiao[i农历年 - m_iMixYear].Substring(i农历月 - 1, 1)可以看出前面的【0-11】位为农历【1-12】月的天数,1代表该月30天,0代表该月29天
//12位是闰月天数,13-14位是闰月月份
//15-18位是该年春节月份日期
//如果想得出其他年份的数组,需要查看日历,自己编写。