年和日转化为天干地支

使用的代码:

_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;
}
在C++中,将秒数转换、月和是一个相对复杂的过程,因为涉及到闰的计算以及不同月份的天数。以下是一个简化的方法,假设每365.25天(考虑闰),每月按30天计算: ```cpp #include <iostream> using namespace std; void convertSeconds(int seconds) { const int secondsInAMinute = 60; const int minutesInAnHour = 60; const int hoursInADay = 24; const int daysInAYear = 365; // 不考虑闰的情况 const int monthsInAYear = 12; int remainingSeconds = seconds; // 计算数 int years = remainingSeconds / (daysInAYear * hoursInADay * minutesInAnHour * secondsInAMinute); remainingSeconds %= (daysInAYear * hoursInADay * minutesInAnHour * secondsInAMinute); // 计算月数 int months = remainingSeconds / (daysInAYear/monthsInAYear * hoursInADay * minutesInAnHour * secondsInAMinute); remainingSeconds %= (daysInAYear/monthsInAYear * hoursInADay * minutesInAnHour * secondsInAMinute); // 计算天数 int days = remainingSeconds / (hoursInADay * minutesInAnHour * secondsInAMinute); remainingSeconds %= (hoursInADay * minutesInAnHour * secondsInAMinute); cout << "Years: " << years << ", Months: " << months << ", Days: " << days << endl; } int main() { int totalSeconds = 1000000000; // 示例秒数 convertSeconds(totalSeconds); return 0; } ``` 这段代码首先定义了各种时间单位之间的转换关系,然后通过连续除法和取余操作计算出给定秒数对应的、月和。注意,这个实现是一个近似值,没有考虑实际的闰规则和不同月份的天数差异。如果需要更精确的转换,就需要实现一个更复杂的算法来处理这些特殊情况。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值