iOS 给三方日历加上农历

首先创建一个农历文件

LunarCalendar.h

  1 //
  2 //  LunarCalendar.h
  3 //  Hnair4iPhone
  4 //
  5 //  Created by yingkong1987 on 13-10-10.
  6 //  Copyright (c) 2013年 yingkong1987. All rights reserved.
  7 //
  8 
  9 #import <Foundation/Foundation.h>
 10 
 11 struct SolarTerm
 12 {
 13     __unsafe_unretained NSString *solarName;
 14     int solarDate;
 15 };
 16 
 17 @interface LunarCalendar : NSObject
 18 {    
 19     NSArray *HeavenlyStems;
 20     NSArray *EarthlyBranches;
 21     NSArray *LunarZodiac;
 22     NSArray *SolarTerms;
 23     NSArray *arrayMonth;
 24     NSArray *arrayDay;
 25 
 26     NSDate *thisdate;
 27     
 28     int year;
 29     int month;
 30     int day;
 31     
 32     int lunarYear;    //农历年
 33     int lunarMonth;    //农历月
 34     int doubleMonth;    //闰月
 35     bool isLeap;    //是否闰月标记
 36     int lunarDay;    //农历日
 37     
 38     struct SolarTerm solarTerm[2];
 39     
 40     NSString *yearHeavenlyStem;
 41     NSString *monthHeavenlyStem;
 42     NSString *dayHeavenlyStem;
 43     
 44     NSString *yearEarthlyBranch;
 45     NSString *monthEarthlyBranch;
 46     NSString *dayEarthlyBranch;
 47     
 48     NSString *monthLunar;
 49     NSString *dayLunar;
 50     
 51     NSString *zodiacLunar;
 52     
 53     NSString *solarTermTitle; 
 54 }
 55 
 56 -(void)loadWithDate:(NSDate *)date;
 57 
 58 -(void)InitializeValue;
 59 -(int)LunarYearDays:(int)y;
 60 -(int)DoubleMonth:(int)y;
 61 -(int)DoubleMonthDays:(int)y;
 62 -(int)MonthDays:(int)y :(int)m;
 63 -(void)ComputeSolarTerm;
 64 
 65 -(double)Term:(int)y :(int)n :(bool)pd;
 66 -(double)AntiDayDifference:(int)y :(double)x;
 67 -(double)EquivalentStandardDay:(int)y :(int)m :(int)d;
 68 -(int)IfGregorian:(int)y :(int)m :(int)d :(int)opt;
 69 -(int)DayDifference:(int)y :(int)m :(int)d;
 70 -(double)Tail:(double)x;
 71 
 72 -(NSString *)MonthLunar;
 73 -(NSString *)DayLunar;
 74 -(NSString *)ZodiacLunar;
 75 -(NSString *)YearHeavenlyStem;
 76 -(NSString *)MonthHeavenlyStem;
 77 -(NSString *)DayHeavenlyStem;
 78 -(NSString *)YearEarthlyBranch;
 79 -(NSString *)MonthEarthlyBranch;
 80 -(NSString *)DayEarthlyBranch;
 81 -(NSString *)SolarTermTitle;
 82 -(bool)IsLeap;
 83 -(int)GregorianYear;
 84 -(int)GregorianMonth;
 85 -(int)GregorianDay;
 86 -(int)Weekday;
 87 -(NSString *)Constellation;
 88 
 89 @end
 90 
 91 
 92 @interface NSDate (LunarCalendar)
 93 
 94 /****************************************************
 95  *@Description:获得NSDate对应的中国日历(农历)的NSDate
 96  *@Params:nil
 97  *@Return:NSDate对应的中国日历(农历)的LunarCalendar
 98  ****************************************************/
 99 - (LunarCalendar *)chineseCalendarDate;
100 
101 
102 @end
View Code

LunarCalendar.m

  1 //
  2 //  LunarCalendar.m
  3 //  Hnair4iPhone
  4 //
  5 //  Created by yingkong1987 on 13-10-10.
  6 //  Copyright (c) 2013年 yingkong1987. All rights reserved.
  7 //
  8 
  9 #import "LunarCalendar.h"
 10 
 11 int LunarCalendarInfo[] = { 0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
 12     0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
 13     0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
 14     0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
 15     0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
 16     0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,
 17     0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
 18     0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,
 19     0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
 20     0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
 21     0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
 22     0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
 23     0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
 24     0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
 25     0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0,
 26     0x14b63};
 27 
 28 @implementation LunarCalendar
 29 
 30 -(id)init
 31 {
 32     HeavenlyStems = [NSArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
 33     EarthlyBranches = [NSArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
 34     LunarZodiac = [NSArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
 35     
 36     SolarTerms = [NSArray arrayWithObjects:@"立春", @"雨水", @"惊蛰", @"春分", @"清明", @"谷雨", @"立夏", @"小满", @"芒种", @"夏至", @"小暑", @"大暑", @"立秋", @"处暑", @"白露", @"秋分", @"寒露", @"霜降", @"立冬", @"小雪", @"大雪", @"冬至", @"小寒", @"大寒", nil];
 37     
 38     arrayMonth = [NSArray arrayWithObjects:@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月",  @"十月", @"冬月", @"腊月", nil];
 39     
 40     arrayDay = [NSArray arrayWithObjects:@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", @"三一", nil];
 41     
 42     return self;
 43 }
 44 
 45 -(void)loadWithDate:(NSDate *)adate
 46 {
 47     if (adate == nil)
 48         [self loadWithDate:[NSDate date]];
 49     else
 50     {
 51         HeavenlyStems = [NSArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
 52         EarthlyBranches = [NSArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
 53         LunarZodiac = [NSArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
 54         
 55         SolarTerms = [NSArray arrayWithObjects:@"立春", @"雨水", @"惊蛰", @"春分", @"清明", @"谷雨", @"立夏", @"小满", @"芒种", @"夏至", @"小暑", @"大暑", @"立秋", @"处暑", @"白露", @"秋分", @"寒露", @"霜降", @"立冬", @"小雪", @"大雪", @"冬至", @"小寒", @"大寒", nil];
 56         
 57         arrayMonth = [NSArray arrayWithObjects:@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月",  @"十月", @"冬月", @"腊月", nil];
 58         
 59         arrayDay = [NSArray arrayWithObjects:@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", @"三一", nil];
 60         
 61         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 62         
 63         [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
 64         [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
 65         
 66         [dateFormatter setDateFormat:@"yyyy"];
 67         year = [[dateFormatter stringFromDate:adate] intValue];
 68         
 69         [dateFormatter setDateFormat:@"MM"];
 70         month = [[dateFormatter stringFromDate:adate] intValue];
 71         
 72         [dateFormatter setDateFormat:@"dd"];
 73         day = [[dateFormatter stringFromDate:adate] intValue];
 74                         
 75         thisdate = adate;
 76     }
 77 }
 78 
 79 -(void)InitializeValue
 80 {
 81     NSString *start = @"1900-01-31";
 82     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 83     [dateFormatter setDateFormat:@"yyyy-MM-dd"];
 84     NSString *end = [dateFormatter stringFromDate:thisdate];
 85     
 86     NSDateFormatter *f = [[NSDateFormatter alloc] init];
 87     [f setDateFormat:@"yyyy-MM-dd"];
 88     NSDate *startDate = [f dateFromString:start];
 89     NSDate *endDate = [f dateFromString:end];
 90     
 91     NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
 92     NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0];
 93     
 94     int dayCyclical=(([components day] + 30)/(86400/(3600*24)))+10;
 95 
 96     int sumdays = [components day];
 97         
 98     int tempdays = 0;
 99 
100     //计算农历年
101     for (lunarYear = 1900; lunarYear < 2050 && sumdays > 0; lunarYear++)
102     {
103         tempdays = [self LunarYearDays:lunarYear];
104         sumdays -= tempdays;
105     }
106     
107     if (sumdays < 0)
108     {
109         sumdays += tempdays;
110         lunarYear--;
111     }
112     
113     //计算闰月
114     doubleMonth = [self DoubleMonth:lunarYear];
115     isLeap = false;
116     
117     //计算农历月
118     for (lunarMonth = 1; lunarMonth < 13 && sumdays > 0; lunarMonth++)
119     {
120         //闰月
121         if (doubleMonth > 0 && lunarMonth == (doubleMonth + 1) && isLeap == false)
122         {
123             --lunarMonth;
124             isLeap = true;
125             tempdays = [self DoubleMonthDays:lunarYear];
126         }
127         else
128         {
129             tempdays = [self MonthDays:lunarYear:lunarMonth];
130         }
131         
132         //解除闰月
133         if (isLeap == true && lunarMonth == (doubleMonth + 1))
134         {
135             isLeap = false;
136         }
137         sumdays -= tempdays;
138     }
139     
140     //计算农历日
141     if (sumdays == 0 && doubleMonth > 0 && lunarMonth == doubleMonth + 1)
142     {
143         if (isLeap)
144         {
145             isLeap = false;
146         }
147         else
148         {
149             isLeap = true;
150             --lunarMonth;
151         }
152     }
153     
154     if (sumdays < 0)
155     {
156         sumdays += tempdays;
157         --lunarMonth;
158     }
159     
160     lunarDay = sumdays + 1;
161     
162     //计算节气
163     [self ComputeSolarTerm];
164     
165     solarTermTitle = @"";
166     for (int i=0; i<2; i++)
167     {
168         NSDateFormatter *currentFormatter = [[NSDateFormatter alloc] init];
169         
170         [currentFormatter setDateFormat:@"yyyyMMdd"];
171         
172         if (solarTerm[i].solarDate == [[currentFormatter stringFromDate:thisdate] intValue])
173             solarTermTitle = solarTerm[i].solarName;
174     }
175 
176     monthLunar = (NSString *)[arrayMonth objectAtIndex:(lunarMonth - 1)];
177     dayLunar = (NSString *)[arrayDay objectAtIndex:(lunarDay - 1)];
178     zodiacLunar = (NSString *)[LunarZodiac objectAtIndex:((lunarYear - 4) % 60 % 12)];
179     
180     yearHeavenlyStem = (NSString *)[HeavenlyStems objectAtIndex:((lunarYear - 4) % 60 % 10)];
181     if ((((year-1900)*12+month+13)%10) == 0)
182         monthHeavenlyStem = (NSString *)[HeavenlyStems objectAtIndex:9];
183     else
184         monthHeavenlyStem = (NSString *)[HeavenlyStems objectAtIndex:(((year-1900)*12+month+13)%10-1)];
185 
186     dayHeavenlyStem = (NSString *)[HeavenlyStems objectAtIndex:(dayCyclical%10)];
187     
188     yearEarthlyBranch = (NSString *)[EarthlyBranches objectAtIndex:((lunarYear - 4) % 60 % 12)];
189     if ((((year-1900)*12+month+13)%12) == 0)
190         monthEarthlyBranch = (NSString *)[EarthlyBranches objectAtIndex:11];
191     else
192         monthEarthlyBranch = (NSString *)[EarthlyBranches objectAtIndex:(((year-1900)*12+month+13)%12-1)];
193     dayEarthlyBranch = (NSString *)[EarthlyBranches objectAtIndex:(dayCyclical%12)];
194 }
195 
196 -(int)LunarYearDays:(int)y
197 {
198     int i, sum = 348;
199     for (i = 0x8000; i > 0x8; i >>= 1)
200     {
201         if ((LunarCalendarInfo[y - 1900] & i) != 0)
202             sum += 1;
203     }
204     return (sum + [self DoubleMonthDays:y]);
205 }
206 
207 -(int)DoubleMonth:(int)y
208 {
209     return (LunarCalendarInfo[y - 1900] & 0xf);
210 }
211 
212 ///返回农历年闰月的天数
213 -(int)DoubleMonthDays:(int)y
214 {
215     if ([self DoubleMonth:y] != 0)
216         return (((LunarCalendarInfo[y - 1900] & 0x10000) != 0) ? 30 : 29);
217     else
218         return (0);
219 }
220 
221 ///返回农历年月份的总天数
222 -(int)MonthDays:(int)y :(int)m
223 {
224     return (((LunarCalendarInfo[y - 1900] & (0x10000 >> m)) != 0) ? 30 : 29);
225 }
226 
227 -(void)ComputeSolarTerm
228 {
229     for (int n = month * 2 - 1; n <= month * 2; n++)
230     {
231         double Termdays = [self Term:year:n:YES];
232         double mdays = [self AntiDayDifference:year:floor(Termdays)];
233         //double sm1 = floor(mdays / 100);
234         int hour = (int)floor((double)[self Tail:Termdays] * 24);
235         int minute = (int)floor((double)([self Tail:Termdays] * 24 - hour) * 60);
236         int tMonth = (int)ceil((double)n / 2);
237         int tday = (int)mdays % 100;
238         
239         if (n >= 3)
240             solarTerm[n - month * 2 + 1].solarName = [SolarTerms objectAtIndex:(n - 3)];
241         else
242             solarTerm[n - month * 2 + 1].solarName = [SolarTerms objectAtIndex:(n + 21)];
243         
244         NSDateComponents *components = [[NSDateComponents alloc] init];
245         [components setYear:year];
246         [components setMonth:tMonth]; 
247         [components setDay:tday];
248         [components setHour:hour];
249         [components setMinute:minute];
250 
251         NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
252         NSDate *ldate = [gregorian dateFromComponents:components];
253         
254 
255         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
256         
257         [dateFormatter setDateFormat:@"yyyyMMdd"];
258                 
259         solarTerm[n - month * 2 + 1].solarDate = [[dateFormatter stringFromDate:ldate] intValue];
260     }
261 }
262 
263 -(double)Tail:(double)x
264 {
265     return x - floor(x);
266 }
267 
268 -(double)Term:(int)y :(int)n :(bool)pd
269 {
270     //儒略日
271     double juD = y * (365.2423112 - 6.4e-14 * (y - 100) * (y - 100) - 3.047e-8 * (y - 100)) + 15.218427 * n + 1721050.71301;
272     
273     //角度
274     double tht = 3e-4 * y - 0.372781384 - 0.2617913325 * n;
275     
276     //年差实均数
277     double yrD = (1.945 * sin(tht) - 0.01206 * sin(2 * tht)) * (1.048994 - 2.583e-5 * y);
278     
279     //朔差实均数
280     double shuoD = -18e-4 * sin(2.313908653 * y - 0.439822951 - 3.0443 * n);
281     
282     double vs = (pd) ? (juD + yrD + shuoD - [self EquivalentStandardDay:y:1:0] - 1721425) : (juD - [self EquivalentStandardDay:y:1:0] - 1721425);
283     return vs;
284 }
285 
286 -(double)AntiDayDifference:(int)y :(double)x
287 {
288     int m = 1;
289     for (int j = 1; j <= 12; j++)
290     {
291         int mL = [self DayDifference:y:(j+1):1] - [self DayDifference:y:j:1];
292         if (x <= mL || j == 12)
293         {
294             m = j;
295             break;
296         }
297         else
298             x -= mL;
299     }
300     return 100 * m + x;
301 }
302 
303 -(double)EquivalentStandardDay:(int)y :(int)m :(int)d
304 {
305     //Julian的等效标准天数
306     double v = (y - 1) * 365 + floor((double)((y - 1) / 4)) + [self DayDifference:y:m:d] - 2;
307     
308     if (y > 1582)
309     {//Gregorian的等效标准天数
310         v += -floor((double)((y - 1) / 100)) + floor((double)((y - 1) / 400)) + 2; 
311     } 
312     return v;
313 }
314 
315 -(int)DayDifference:(int)y :(int)m :(int)d
316 {
317     int ifG = [self IfGregorian:y:m:d:1];
318     //NSArray *monL = [NSArray arrayWithObjects:, nil];
319     NSInteger monL[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
320     if (ifG == 1)
321     {
322         if ((y % 100 != 0 && y % 4 == 0) || (y % 400 == 0))
323             monL[2] += 1;
324         else
325         {
326             if (y % 4 == 0)
327                 monL[2] += 1;
328         }
329     }
330 
331     int v = 0;
332     
333     for (int i = 0; i <= m - 1; i++)
334         v += monL[i];
335 
336     v += d;
337     if (y == 1582)
338     {
339         if (ifG == 1)
340             v -= 10;
341         if (ifG == -1)
342             v = 0;  //infinity 
343     }
344     return v;
345 }
346 
347 -(int)IfGregorian:(int)y :(int)m :(int)d :(int)opt
348 {
349     if (opt == 1)
350     {
351         if (y > 1582 || (y == 1582 && m > 10) || (y == 1582 && m == 10 && d > 14))
352             return (1);     //Gregorian
353         else
354             if (y == 1582 && m == 10 && d >= 5 && d <= 14)
355                 return (-1);  //
356             else
357                 return (0);  //Julian
358     }
359     
360     if (opt == 2)
361         return (1);     //Gregorian
362     if (opt == 3)
363         return (0);     //Julian
364     return (-1);
365 }
366 
367 -(NSString *)MonthLunar
368 {
369     return monthLunar;
370 }
371 
372 -(NSString *)DayLunar
373 {
374     return dayLunar;
375 }
376 
377 -(NSString *)ZodiacLunar
378 {
379     return zodiacLunar;
380 }
381 
382 -(NSString *)YearHeavenlyStem
383 {
384     return yearHeavenlyStem;
385 }
386 
387 -(NSString *)MonthHeavenlyStem
388 {
389     return monthHeavenlyStem;
390 }
391 
392 -(NSString *)DayHeavenlyStem
393 {
394     return dayHeavenlyStem;
395 }
396 
397 -(NSString *)YearEarthlyBranch
398 {
399     return yearEarthlyBranch;
400 }
401 
402 -(NSString *)MonthEarthlyBranch
403 {
404     return monthEarthlyBranch;
405 }
406 
407 -(NSString *)DayEarthlyBranch
408 {
409     return dayEarthlyBranch;
410 }
411 
412 -(NSString *)SolarTermTitle
413 {
414     return solarTermTitle;
415 }
416 
417 -(bool)IsLeap
418 {
419     return isLeap;
420 }
421 
422 -(int)GregorianYear
423 {
424     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
425     [formatter setDateFormat:@"yyyy"];
426     int ret = [[formatter stringFromDate:thisdate] intValue];
427 
428     return ret;
429 }
430 
431 -(int)GregorianMonth
432 {
433     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
434     [formatter setDateFormat:@"MM"];
435     int ret = [[formatter stringFromDate:thisdate] intValue];
436 
437     return ret;
438 }
439 
440 -(int)GregorianDay
441 {
442     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
443     [formatter setDateFormat:@"dd"];
444     int ret = [[formatter stringFromDate:thisdate] intValue];
445 
446     return ret;
447 }
448 
449 -(int)Weekday
450 {
451     NSCalendar* cal = [NSCalendar currentCalendar];
452     NSDateComponents* weekday = [cal components:NSWeekdayCalendarUnit fromDate:thisdate];
453     return [weekday weekday];
454 }
455 
456 -(NSString *)Constellation
457 {
458     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
459     [formatter setDateFormat:@"MMdd"];
460     int intConstellation = [[formatter stringFromDate:thisdate] intValue];
461     
462     if (intConstellation >= 120 && intConstellation <= 218)
463         return @"Aquarius";
464     else if (intConstellation >= 219 && intConstellation <= 320)
465         return @"Pisces";
466     else if (intConstellation >= 321 && intConstellation <= 420)
467         return @"Aries";
468     else if (intConstellation >= 421 && intConstellation <= 520)
469         return @"Taurus";
470     else if (intConstellation >= 521 && intConstellation <= 621)
471         return @"Gemini";
472     else if (intConstellation >= 622 && intConstellation <= 722)
473         return @"Cancer";
474     else if (intConstellation >= 723 && intConstellation <= 822)
475         return @"Leo";
476     else if (intConstellation >= 823 && intConstellation <= 922)
477         return @"Virgo";
478     else if (intConstellation >= 923 && intConstellation <= 1022)
479         return @"Libra";
480     else if (intConstellation >= 1023 && intConstellation <= 1121)
481         return @"Scorpio";
482     else if (intConstellation >= 1122 && intConstellation <= 1221)
483         return @"Sagittarius";
484     else
485         return @"Capricorn";
486 }
487 
488 @end
489 
490 
491 @implementation NSDate (LunarCalendar)
492 
493 /****************************************************
494  *@Description:获得NSDate对应的中国日历(农历)的NSDate
495  *@Params:nil
496  *@Return:NSDate对应的中国日历(农历)的LunarCalendar
497  ****************************************************/
498 - (LunarCalendar *)chineseCalendarDate
499 {
500     LunarCalendar *lunarCalendar = [[LunarCalendar alloc] init];
501     [lunarCalendar loadWithDate:self];
502     [lunarCalendar InitializeValue];
503     
504     return lunarCalendar;
505 }
506 
507 @end
View Code

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

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

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

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

然后如红框使用

效果如图:

原文地址:https://www.cnblogs.com/yingkong1987/p/3366554.html