iOS 给三方日历加上农历

首先创建一个农历文件

LunarCalendar.h

 //
// LunarCalendar.h
// Hnair4iPhone
//
// Created by yingkong1987 on 13-10-10.
// Copyright (c) 2013年 yingkong1987. All rights reserved.
// #import <Foundation/Foundation.h> struct SolarTerm
{
__unsafe_unretained NSString *solarName;
int solarDate;
}; @interface LunarCalendar : NSObject
{
NSArray *HeavenlyStems;
NSArray *EarthlyBranches;
NSArray *LunarZodiac;
NSArray *SolarTerms;
NSArray *arrayMonth;
NSArray *arrayDay; NSDate *thisdate; int year;
int month;
int day; int lunarYear; //农历年
int lunarMonth; //农历月
int doubleMonth; //闰月
bool isLeap; //是否闰月标记
int lunarDay; //农历日 struct SolarTerm solarTerm[]; NSString *yearHeavenlyStem;
NSString *monthHeavenlyStem;
NSString *dayHeavenlyStem; NSString *yearEarthlyBranch;
NSString *monthEarthlyBranch;
NSString *dayEarthlyBranch; NSString *monthLunar;
NSString *dayLunar; NSString *zodiacLunar; NSString *solarTermTitle;
} -(void)loadWithDate:(NSDate *)date; -(void)InitializeValue;
-(int)LunarYearDays:(int)y;
-(int)DoubleMonth:(int)y;
-(int)DoubleMonthDays:(int)y;
-(int)MonthDays:(int)y :(int)m;
-(void)ComputeSolarTerm; -(double)Term:(int)y :(int)n :(bool)pd;
-(double)AntiDayDifference:(int)y :(double)x;
-(double)EquivalentStandardDay:(int)y :(int)m :(int)d;
-(int)IfGregorian:(int)y :(int)m :(int)d :(int)opt;
-(int)DayDifference:(int)y :(int)m :(int)d;
-(double)Tail:(double)x; -(NSString *)MonthLunar;
-(NSString *)DayLunar;
-(NSString *)ZodiacLunar;
-(NSString *)YearHeavenlyStem;
-(NSString *)MonthHeavenlyStem;
-(NSString *)DayHeavenlyStem;
-(NSString *)YearEarthlyBranch;
-(NSString *)MonthEarthlyBranch;
-(NSString *)DayEarthlyBranch;
-(NSString *)SolarTermTitle;
-(bool)IsLeap;
-(int)GregorianYear;
-(int)GregorianMonth;
-(int)GregorianDay;
-(int)Weekday;
-(NSString *)Constellation; @end @interface NSDate (LunarCalendar) /****************************************************
*@Description:获得NSDate对应的中国日历(农历)的NSDate
*@Params:nil
*@Return:NSDate对应的中国日历(农历)的LunarCalendar
****************************************************/
- (LunarCalendar *)chineseCalendarDate; @end

LunarCalendar.m

 //
// LunarCalendar.m
// Hnair4iPhone
//
// Created by yingkong1987 on 13-10-10.
// Copyright (c) 2013年 yingkong1987. All rights reserved.
// #import "LunarCalendar.h" int LunarCalendarInfo[] = { 0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,
0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,
0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0,
0x14b63}; @implementation LunarCalendar -(id)init
{
HeavenlyStems = [NSArray arrayWithObjects:@"甲",@"乙",@"丙",@"丁",@"戊",@"己",@"庚",@"辛",@"壬",@"癸",nil];
EarthlyBranches = [NSArray arrayWithObjects:@"子",@"丑",@"寅",@"卯",@"辰",@"巳",@"午",@"未",@"申",@"酉",@"戌",@"亥",nil];
LunarZodiac = [NSArray arrayWithObjects:@"鼠",@"牛",@"虎",@"兔",@"龙",@"蛇",@"马",@"羊",@"猴",@"鸡",@"狗",@"猪",nil]; SolarTerms = [NSArray arrayWithObjects:@"立春", @"雨水", @"惊蛰", @"春分", @"清明", @"谷雨", @"立夏", @"小满", @"芒种", @"夏至", @"小暑", @"大暑", @"立秋", @"处暑", @"白露", @"秋分", @"寒露", @"霜降", @"立冬", @"小雪", @"大雪", @"冬至", @"小寒", @"大寒", nil]; arrayMonth = [NSArray arrayWithObjects:@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月", @"十月", @"冬月", @"腊月", nil]; arrayDay = [NSArray arrayWithObjects:@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", @"三一", nil]; return self;
} -(void)loadWithDate:(NSDate *)adate
{
if (adate == nil)
[self loadWithDate:[NSDate date]];
else
{
HeavenlyStems = [NSArray arrayWithObjects:@"甲",@"乙",@"丙",@"丁",@"戊",@"己",@"庚",@"辛",@"壬",@"癸",nil];
EarthlyBranches = [NSArray arrayWithObjects:@"子",@"丑",@"寅",@"卯",@"辰",@"巳",@"午",@"未",@"申",@"酉",@"戌",@"亥",nil];
LunarZodiac = [NSArray arrayWithObjects:@"鼠",@"牛",@"虎",@"兔",@"龙",@"蛇",@"马",@"羊",@"猴",@"鸡",@"狗",@"猪",nil]; SolarTerms = [NSArray arrayWithObjects:@"立春", @"雨水", @"惊蛰", @"春分", @"清明", @"谷雨", @"立夏", @"小满", @"芒种", @"夏至", @"小暑", @"大暑", @"立秋", @"处暑", @"白露", @"秋分", @"寒露", @"霜降", @"立冬", @"小雪", @"大雪", @"冬至", @"小寒", @"大寒", nil]; arrayMonth = [NSArray arrayWithObjects:@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月", @"十月", @"冬月", @"腊月", nil]; arrayDay = [NSArray arrayWithObjects:@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", @"三一", nil]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle]; [dateFormatter setDateFormat:@"yyyy"];
year = [[dateFormatter stringFromDate:adate] intValue]; [dateFormatter setDateFormat:@"MM"];
month = [[dateFormatter stringFromDate:adate] intValue]; [dateFormatter setDateFormat:@"dd"];
day = [[dateFormatter stringFromDate:adate] intValue]; thisdate = adate;
}
} -(void)InitializeValue
{
NSString *start = @"1900-01-31";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *end = [dateFormatter stringFromDate:thisdate]; NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end]; NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:]; int dayCyclical=(([components day] + )/(/(*)))+; int sumdays = [components day]; int tempdays = ; //计算农历年
for (lunarYear = ; lunarYear < && sumdays > ; lunarYear++)
{
tempdays = [self LunarYearDays:lunarYear];
sumdays -= tempdays;
} if (sumdays < )
{
sumdays += tempdays;
lunarYear--;
} //计算闰月
doubleMonth = [self DoubleMonth:lunarYear];
isLeap = false; //计算农历月
for (lunarMonth = ; lunarMonth < && sumdays > ; lunarMonth++)
{
//闰月
if (doubleMonth > && lunarMonth == (doubleMonth + ) && isLeap == false)
{
--lunarMonth;
isLeap = true;
tempdays = [self DoubleMonthDays:lunarYear];
}
else
{
tempdays = [self MonthDays:lunarYear:lunarMonth];
} //解除闰月
if (isLeap == true && lunarMonth == (doubleMonth + ))
{
isLeap = false;
}
sumdays -= tempdays;
} //计算农历日
if (sumdays == && doubleMonth > && lunarMonth == doubleMonth + )
{
if (isLeap)
{
isLeap = false;
}
else
{
isLeap = true;
--lunarMonth;
}
} if (sumdays < )
{
sumdays += tempdays;
--lunarMonth;
} lunarDay = sumdays + ; //计算节气
[self ComputeSolarTerm]; solarTermTitle = @"";
for (int i=; i<; i++)
{
NSDateFormatter *currentFormatter = [[NSDateFormatter alloc] init]; [currentFormatter setDateFormat:@"yyyyMMdd"]; if (solarTerm[i].solarDate == [[currentFormatter stringFromDate:thisdate] intValue])
solarTermTitle = solarTerm[i].solarName;
} monthLunar = (NSString *)[arrayMonth objectAtIndex:(lunarMonth - )];
dayLunar = (NSString *)[arrayDay objectAtIndex:(lunarDay - )];
zodiacLunar = (NSString *)[LunarZodiac objectAtIndex:((lunarYear - ) % % )]; yearHeavenlyStem = (NSString *)[HeavenlyStems objectAtIndex:((lunarYear - ) % % )];
if ((((year-)*+month+)%) == )
monthHeavenlyStem = (NSString *)[HeavenlyStems objectAtIndex:];
else
monthHeavenlyStem = (NSString *)[HeavenlyStems objectAtIndex:(((year-)*+month+)%-)]; dayHeavenlyStem = (NSString *)[HeavenlyStems objectAtIndex:(dayCyclical%)]; yearEarthlyBranch = (NSString *)[EarthlyBranches objectAtIndex:((lunarYear - ) % % )];
if ((((year-)*+month+)%) == )
monthEarthlyBranch = (NSString *)[EarthlyBranches objectAtIndex:];
else
monthEarthlyBranch = (NSString *)[EarthlyBranches objectAtIndex:(((year-)*+month+)%-)];
dayEarthlyBranch = (NSString *)[EarthlyBranches objectAtIndex:(dayCyclical%)];
} -(int)LunarYearDays:(int)y
{
int i, sum = ;
for (i = 0x8000; i > 0x8; i >>= )
{
if ((LunarCalendarInfo[y - ] & i) != )
sum += ;
}
return (sum + [self DoubleMonthDays:y]);
} -(int)DoubleMonth:(int)y
{
return (LunarCalendarInfo[y - ] & 0xf);
} ///返回农历年闰月的天数
-(int)DoubleMonthDays:(int)y
{
if ([self DoubleMonth:y] != )
return (((LunarCalendarInfo[y - ] & 0x10000) != ) ? : );
else
return ();
} ///返回农历年月份的总天数
-(int)MonthDays:(int)y :(int)m
{
return (((LunarCalendarInfo[y - ] & (0x10000 >> m)) != ) ? : );
} -(void)ComputeSolarTerm
{
for (int n = month * - ; n <= month * ; n++)
{
double Termdays = [self Term:year:n:YES];
double mdays = [self AntiDayDifference:year:floor(Termdays)];
//double sm1 = floor(mdays / 100);
int hour = (int)floor((double)[self Tail:Termdays] * );
int minute = (int)floor((double)([self Tail:Termdays] * - hour) * );
int tMonth = (int)ceil((double)n / );
int tday = (int)mdays % ; if (n >= )
solarTerm[n - month * + ].solarName = [SolarTerms objectAtIndex:(n - )];
else
solarTerm[n - month * + ].solarName = [SolarTerms objectAtIndex:(n + )]; NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:year];
[components setMonth:tMonth];
[components setDay:tday];
[components setHour:hour];
[components setMinute:minute]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *ldate = [gregorian dateFromComponents:components]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyyMMdd"]; solarTerm[n - month * + ].solarDate = [[dateFormatter stringFromDate:ldate] intValue];
}
} -(double)Tail:(double)x
{
return x - floor(x);
} -(double)Term:(int)y :(int)n :(bool)pd
{
//儒略日
double juD = y * (365.2423112 - 6.4e-14 * (y - ) * (y - ) - 3.047e-8 * (y - )) + 15.218427 * n + 1721050.71301; //角度
double tht = 3e- * y - 0.372781384 - 0.2617913325 * n; //年差实均数
double yrD = (1.945 * sin(tht) - 0.01206 * sin( * tht)) * (1.048994 - 2.583e-5 * y); //朔差实均数
double shuoD = -18e- * sin(2.313908653 * y - 0.439822951 - 3.0443 * n); double vs = (pd) ? (juD + yrD + shuoD - [self EquivalentStandardDay:y::] - ) : (juD - [self EquivalentStandardDay:y::] - );
return vs;
} -(double)AntiDayDifference:(int)y :(double)x
{
int m = ;
for (int j = ; j <= ; j++)
{
int mL = [self DayDifference:y:(j+):] - [self DayDifference:y:j:];
if (x <= mL || j == )
{
m = j;
break;
}
else
x -= mL;
}
return * m + x;
} -(double)EquivalentStandardDay:(int)y :(int)m :(int)d
{
//Julian的等效标准天数
double v = (y - ) * + floor((double)((y - ) / )) + [self DayDifference:y:m:d] - ; if (y > )
{//Gregorian的等效标准天数
v += -floor((double)((y - ) / )) + floor((double)((y - ) / )) + ;
}
return v;
} -(int)DayDifference:(int)y :(int)m :(int)d
{
int ifG = [self IfGregorian:y:m:d:];
//NSArray *monL = [NSArray arrayWithObjects:, nil];
NSInteger monL[] = {, , , , , , , , , , , , };
if (ifG == )
{
if ((y % != && y % == ) || (y % == ))
monL[] += ;
else
{
if (y % == )
monL[] += ;
}
} int v = ; for (int i = ; i <= m - ; i++)
v += monL[i]; v += d;
if (y == )
{
if (ifG == )
v -= ;
if (ifG == -)
v = ; //infinity
}
return v;
} -(int)IfGregorian:(int)y :(int)m :(int)d :(int)opt
{
if (opt == )
{
if (y > || (y == && m > ) || (y == && m == && d > ))
return (); //Gregorian
else
if (y == && m == && d >= && d <= )
return (-); //空
else
return (); //Julian
} if (opt == )
return (); //Gregorian
if (opt == )
return (); //Julian
return (-);
} -(NSString *)MonthLunar
{
return monthLunar;
} -(NSString *)DayLunar
{
return dayLunar;
} -(NSString *)ZodiacLunar
{
return zodiacLunar;
} -(NSString *)YearHeavenlyStem
{
return yearHeavenlyStem;
} -(NSString *)MonthHeavenlyStem
{
return monthHeavenlyStem;
} -(NSString *)DayHeavenlyStem
{
return dayHeavenlyStem;
} -(NSString *)YearEarthlyBranch
{
return yearEarthlyBranch;
} -(NSString *)MonthEarthlyBranch
{
return monthEarthlyBranch;
} -(NSString *)DayEarthlyBranch
{
return dayEarthlyBranch;
} -(NSString *)SolarTermTitle
{
return solarTermTitle;
} -(bool)IsLeap
{
return isLeap;
} -(int)GregorianYear
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
int ret = [[formatter stringFromDate:thisdate] intValue]; return ret;
} -(int)GregorianMonth
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM"];
int ret = [[formatter stringFromDate:thisdate] intValue]; return ret;
} -(int)GregorianDay
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd"];
int ret = [[formatter stringFromDate:thisdate] intValue]; return ret;
} -(int)Weekday
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* weekday = [cal components:NSWeekdayCalendarUnit fromDate:thisdate];
return [weekday weekday];
} -(NSString *)Constellation
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMdd"];
int intConstellation = [[formatter stringFromDate:thisdate] intValue]; if (intConstellation >= && intConstellation <= )
return @"Aquarius";
else if (intConstellation >= && intConstellation <= )
return @"Pisces";
else if (intConstellation >= && intConstellation <= )
return @"Aries";
else if (intConstellation >= && intConstellation <= )
return @"Taurus";
else if (intConstellation >= && intConstellation <= )
return @"Gemini";
else if (intConstellation >= && intConstellation <= )
return @"Cancer";
else if (intConstellation >= && intConstellation <= )
return @"Leo";
else if (intConstellation >= && intConstellation <= )
return @"Virgo";
else if (intConstellation >= && intConstellation <= )
return @"Libra";
else if (intConstellation >= && intConstellation <= )
return @"Scorpio";
else if (intConstellation >= && intConstellation <= )
return @"Sagittarius";
else
return @"Capricorn";
} @end @implementation NSDate (LunarCalendar) /****************************************************
*@Description:获得NSDate对应的中国日历(农历)的NSDate
*@Params:nil
*@Return:NSDate对应的中国日历(农历)的LunarCalendar
****************************************************/
- (LunarCalendar *)chineseCalendarDate
{
LunarCalendar *lunarCalendar = [[LunarCalendar alloc] init];
[lunarCalendar loadWithDate:self];
[lunarCalendar InitializeValue]; return lunarCalendar;
} @end

然后以我昨天使用的TimesSquare为例.

先找到每个日期按钮生成的地方.TimesSquare日历的就是在TSQCalendarRowCell的createDayButtons方法.

找到后,在里面增加一个label.如红框所示

iOS 给三方日历加上农历

记得import刚创建的农历文件LunarCalendar.h

然后如红框使用

iOS 给三方日历加上农历

效果如图:

iOS 给三方日历加上农历

上一篇:Python安装、卸载第三方模块


下一篇:带节日和农历的js日历 带农历的脚本: