使用的代码:
_yearLabel.text = [time getYearHeavenlyStemsEarthlyBranches];
NSString+Extension.h文件
#import <Foundation/Foundation.h> @interface NSString (Extension) /** * 把绝对时间的具体一天转换为天干地址,非枚举法 */ -(NSString *)getDayHeavenlyStemsEarthlyBranches; /** * 把绝对时间的具体年份转换为天干地支 */ -(NSString *)getYearHeavenlyStemsEarthlyBranches; /** * 把绝对时间的日转换为十二生肖,枚举法 */ -(NSString *)getZodiac; @end
NSString+Extension.m文件
#import "NSString+Extension.h" @implementation NSString (Extension) #pragma mark - 把时间戳格式化为 yyyy的格式 - (NSString *)dateFomatterStringWithY { FLDDLogDebug(@"函数"); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp]; } #pragma mark - 把时间戳格式化为 MM的格式 - (NSString *)dateFomatterStringWithM { FLDDLogDebug(@"函数"); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"MM"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp]; } #pragma mark - 把时间戳格式化为 yyyy的格式 - (NSString *)dateFomatterStringWithD { FLDDLogDebug(@"函数"); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"dd"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp]; } #pragma mark - 把绝对时间的日转换为十二生肖,枚举法 -(NSString *)getZodiac { if([self longLongValue] < 0) { return @"未知时间"; } long long year = [[self dateFomatterStringWithY] longLongValue]; NSArray *zodiacArr = [NSArray arrayWithObjects:@"子", @"丑",@"寅",@"卯",@"辰",@"巳",@"午",@"未",@"申",@"酉",@"戌",@"亥",nil]; NSInteger n = (year - 1960)/12; for(NSInteger j = 1960+n*12; (j <= year) && (j < 3000); ) { for(NSInteger i = 0; (i < 12) && (j + i <= year) ; i++) { if(j == year) { return zodiacArr[i]; } j++; } } return nil; } #pragma mark - 把绝对时间的具体一天转换为天干地址 -(NSString *)getDayHeavenlyStemsEarthlyBranches { if([self longLongValue] < 0) { return @"未知时间"; } NSString *yearStr = [self dateFomatterStringWithY]; if(yearStr.length != 4) { return @"未知时间"; } //注意是到下标为1的结束,不包含下标为2的数据 NSInteger C = [[yearStr substringToIndex:2] integerValue]; //注意是从下标为2的开始的数据 NSInteger y = [[yearStr substringFromIndex:2] integerValue]; NSInteger m = [[self dateFomatterStringWithM] integerValue]; NSInteger d = [[self dateFomatterStringWithD] integerValue]; NSInteger i = m % 2 == 0 ? 6 : 0; NSArray *heavenlyStemsArr = [NSArray arrayWithObjects:@"葵", @"甲", @"乙",@"丙",@"丁",@"戊",@"己",@"庚",@"辛",@"壬",nil]; NSArray *zodiacArr = [NSArray arrayWithObjects:@"亥",@"子", @"丑",@"寅",@"卯",@"辰",@"巳",@"午",@"未",@"申",@"酉",@"戌",nil]; if((m == 1) || (m == 2)) { m = m + 12; NSInteger yearValue = [yearStr integerValue] - 1; NSString * yearNewStr = [NSString stringWithFormat:@"%4ld",yearValue]; C = [[yearNewStr substringToIndex:1] integerValue]; y = [[yearNewStr substringFromIndex:2] integerValue]; } NSInteger g = (4*C + C/4 + 5*y + y/4 + 3*(m + 1)/5 + d - 3)%10; NSInteger z = (8*C + C/4 + 5*y + y/4 + 3*(m + 1)/5 + d + 7 + i)%12; if((g < 0) || (z < 0)) { return @"未知时间"; } return [NSString stringWithFormat:@"%@%@",heavenlyStemsArr[g],zodiacArr[z]]; } #pragma mark - 把绝对时间的具体年份转换为天干地址 -(NSString *)getYearHeavenlyStemsEarthlyBranches { if([self longLongValue] < 0) { return @"未知时间"; } NSString *yearStr = [self dateFomatterStringWithY]; if(yearStr.length != 4) { return @"未知时间"; } NSInteger y = [yearStr integerValue]; NSArray *heavenlyStemsArr = [NSArray arrayWithObjects:@"甲", @"乙",@"丙",@"丁",@"戊",@"己",@"庚",@"辛",@"壬",@"葵",nil]; NSArray *zodiacArr = [NSArray arrayWithObjects:@"子", @"丑",@"寅",@"卯",@"辰",@"巳",@"午",@"未",@"申",@"酉",@"戌",@"亥",nil]; NSMutableArray *heavenlyStemsEarthlyBranchesArr = [NSMutableArray array]; NSInteger n = 0; NSInteger k = 0; for(;(n < 60) ;) { for(NSInteger j = 0;(n < 60) ; ) { j = 0; for(;((n < 60) && (k < 12) && (j < 10)) ; k++) { NSString * unit = [NSString stringWithFormat:@"%@%@",heavenlyStemsArr[j], zodiacArr[k%12]]; [heavenlyStemsEarthlyBranchesArr addSafeObject:unit]; n++; j++; if((j< 10) && (k >= 11)) { k = -1; } } } } // for (NSString *str in heavenlyStemsEarthlyBranchesArr) { // NSLog(@"%@,", str); // } return heavenlyStemsEarthlyBranchesArr[(y - 1624)%60]; } @end
月转化为天干地址和年转化为天干地支类似,我就不累述了。
计算指定日期计算距离现在的天数的算法:
-(NSString *)currentDate : (NSString *)startDay { if(!startDay) { return nil; } // 日期格式化类 NSDateFormatter *format = [[NSDateFormatter alloc] init]; // 设置日期格式 为了转换成功 format.dateFormat = @"yyyy-MM-dd"; NSDate *data = [format dateFromString:startDay]; NSString *startDayString = [format stringFromDate:data]; NSString *nowDayString = [format stringFromDate:[NSDate date]]; NSTimeInterval timeInterval = [data timeIntervalSinceDate:[NSDate date]]; long long intervalDay = fabs(timeInterval/3600/24); //2009-08-18到2009-08-19算一天 NSString *result = [NSString stringWithFormat:@"开始时间:%@ 今天:%@ 相差天数:%lld天", startDayString, nowDayString, intervalDay]; NSLog(@"%@", result); return result; }