年和日转化为天干地支

来源:互联网 发布:windows预览体验注册 编辑:程序博客网 时间:2024/04/29 11:52

使用的代码:

_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

月转化为天干地址和年转化为天干地支类似,我就不累述了。

0 0
原创粉丝点击