NSString *dateTime = [self LunarForSolarYear:year Month:month Day:day];
//农历转换函数
- (NSString *)LunarForSolarYear:(int)wCurYear Month:(int)wCurMonth Day:(NSInteger)wCurDay {
//农历日期名
NSArray *cDayName = [NSArray arrayWithObjects:@"*",@"初一",@"初二",@"初三",@"初四",@"初五",@"初六",@"初七",@"初八",@"初九",@"初十",
@"十一",@"十二",@"十三",@"十四",@"十五",@"十六",@"十七",@"十八",@"十九",@"二十",
@"廿一",@"廿二",@"廿三",@"廿四",@"廿五",@"廿六",@"廿七",@"廿八",@"廿九",@"三十",nil];
//农历月份名
NSArray *cMonName = [NSArray arrayWithObjects:@"*",@"正",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"腊",nil];
//公历每月前面的天数
const int wMonthAdd[] = {,,,,,,,,,,,};
//农历数据
const int wNongliData[] = {,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,};
static int nTheDate,nIsEnd,m,k,n,i,nBit;
//计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)
nTheDate = (wCurYear - ) * + (wCurYear - ) / + wCurDay + wMonthAdd[wCurMonth - ] - ;
if((!(wCurYear % )) && (wCurMonth > ))
nTheDate = nTheDate + ;
//计算农历天干、地支、月、日
nIsEnd = ;
m = ;
while(nIsEnd != ) {
if(wNongliData[m] < )
k = ;
else
k = ;
n = k;
while(n>=) {
//获取wNongliData(m)的第n个二进制位的值
nBit = wNongliData[m];
for(i=;i< n+;i++)
nBit = nBit/;
nBit = nBit % ;
if (nTheDate <= ( + nBit)) {
nIsEnd = ;
break;
}
nTheDate = nTheDate - - nBit;
n = n - ;
}
if(nIsEnd)
break;
m = m + ;
}
wCurYear = + m;
wCurMonth = k - n + ;
wCurDay = nTheDate;
if (k == ) {
if (wCurMonth == wNongliData[m] / + )
wCurMonth = - wCurMonth;
else if (wCurMonth > wNongliData[m] / + )
wCurMonth = wCurMonth - ;
}
//生成农历月
NSString *szNongliMonth;
if (wCurMonth < ){
szNongliMonth = [NSString stringWithFormat:@"闰%@",(NSString *)[cMonName objectAtIndex:- * wCurMonth]];
}else{
szNongliMonth = (NSString *)[cMonName objectAtIndex:wCurMonth];
}
//生成农历日
NSString *szNongliDay = [cDayName objectAtIndex:wCurDay];
//合并
NSString *lunarDate = [NSString stringWithFormat:@"农历%@月%@",szNongliMonth,szNongliDay];
return lunarDate;
}