class ClassNongLi
{
private string[] sNongLiList;
private string[] sTGDZList;
private string sShuXiangList;
private string sNongLiMonthsList;
private string sNongLiDaysList;
private int iMaxGongLiYear;
private int iMinGongLiYear;
public ClassNongLi()
{
sNongLiList = 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 "
};
sShuXiangList = "鼠牛虎兔龙蛇马羊猴鸡狗猪 ";
sNongLiMonthsList = "正二三四五六七八九十寒腊 ";
sNongLiDaysList = "初一初二初三初四初五初六初七初八初九初十十一十二十三十四十五十六十七十八十九二十廿一廿二廿三廿四廿五廿六廿七廿八廿九三十 ";
iMaxGongLiYear = 2011;
iMinGongLiYear = 1900;
string strTG = "甲乙丙丁戊已庚辛壬癸 ";
string strDZ = "子丑寅卯辰巳午未申酉戌亥 ";
sTGDZList = new string[60];
for (int i = 0; i < 60; i++)
{
sTGDZList[i] = strTG.Substring(i % 10, 1) + strDZ.Substring(i % 12, 1);
}
}
public string GetNongLiDate(int iGongLiYear, int iGongLiMonth, int iGongLiDay)
{
if ((iGongLiYear < iMinGongLiYear) || (iGongLiYear > iMaxGongLiYear))
{ //如果不是有效公历日期,退出。
return "无效公历年份 ";
}
// 计算农历年
int iNongLiYear;
int iNongLiMonth;
int iNongLiDay;
iNongLiYear = iGongLiYear;
// 农历新年月份
iNongLiMonth = Convert.ToInt32((sNongLiList[iNongLiYear - iMinGongLiYear].Substring(15, 2)));
// 农历新年日子
iNongLiDay = Convert.ToInt32((sNongLiList[iNongLiYear - iMinGongLiYear].Substring(17, 2))); ;
if ((iGongLiMonth < iNongLiMonth) || ((iGongLiMonth == iNongLiMonth) && (iGongLiDay < iNongLiDay)))
{
iNongLiYear--;
// 农历新年月份
iNongLiMonth = Convert.ToInt32((sNongLiList[iNongLiYear - iMinGongLiYear].Substring(15, 2)));
// 农历新年日子
iNongLiDay = Convert.ToInt32((sNongLiList[iNongLiYear - iMinGongLiYear].Substring(17, 2))); ;
}
// 计算农历月
DateTime dtGongLiDate = new DateTime(iGongLiYear, iGongLiMonth, iGongLiDay);
DateTime dtNongLiDate = new DateTime(iNongLiYear, iNongLiMonth, iNongLiDay);
TimeSpan tsDateDev = dtGongLiDate - dtNongLiDate;
int iDays = tsDateDev.Days;
iNongLiMonth = 1;
iNongLiDay = 1;
bool bLeapMonth = false;
for (int i = 0; i < iDays; i++)
{
iNongLiDay++;
if (iNongLiDay == 30 + Convert.ToInt32(sNongLiList[iNongLiYear - iMinGongLiYear].Substring(iNongLiMonth - 1, 1)) ||
(bLeapMonth && (iNongLiDay == 30 + Convert.ToInt32(sNongLiList[iNongLiYear - iMinGongLiYear].Substring(12, 1)))))
{
if ((bLeapMonth == false) && (iNongLiMonth == Convert.ToInt32(sNongLiList[iNongLiYear - iMinGongLiYear].Substring(13, 2))))
{
bLeapMonth = true;
}
else
{
bLeapMonth = false;
iNongLiMonth++;
}
iNongLiDay = 1;
}
//else
//{
// //ddddddddddddd
//}
}
// 计算农历日
string strNongLiDay = sNongLiDaysList.Substring((iNongLiDay - 1) * 2, 2);
// 计算农历月
string strNongLiMonth = sNongLiMonthsList.Substring(iNongLiMonth - 1, 1) + "月 ";
if (bLeapMonth)
{
strNongLiMonth = "闰 " + strNongLiMonth;
}
// 农历年
string strNongLiYear = Convert.ToString(iNongLiYear, 10) + "年 ";
// 计算天干地支
string strTGDZ = sTGDZList[(iNongLiYear - 4) % 60];
// 计算属相
string strShuXiang = sShuXiangList.Substring((iNongLiYear - 4) % 12, 1);
// 返回农历日期
return strShuXiang + ", " + strTGDZ + ", " + strNongLiYear + strNongLiMonth + strNongLiDay;
}
}