为了账号安全,请及时绑定邮箱和手机立即绑定

如何在iPhone上将NSTimeInterval分解为年,月,日,小时,分钟和秒?

如何在iPhone上将NSTimeInterval分解为年,月,日,小时,分钟和秒?

iOS
慕虎7371278 2019-12-20 10:57:25
我的时间跨度跨度数年,并且我希望所有时间成分都从一年到几秒。我的第一个想法是将时间间隔整数除以一年中的秒数,再减去连续运行的秒数,再除以一个月中的秒数,再减去运行总数,以此类推。这似乎有些令人费解,而且我读到,每当您做一些看起来令人费解的事情时,可能都有一个内置方法。在那儿?我将Alex的第二种方法集成到了我的代码中。它在我的界面中由UIDatePicker调用的方法中。NSDate *now = [NSDate date];NSDate *then = self.datePicker.date;NSTimeInterval howLong = [now timeIntervalSinceDate:then];NSDate *date = [NSDate dateWithTimeIntervalSince1970:howLong];NSString *dateStr = [date description];const char *dateStrPtr = [dateStr UTF8String];int year, month, day, hour, minute, sec;sscanf(dateStrPtr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &sec);year -= 1970;NSLog(@"%d years\n%d months\n%d days\n%d hours\n%d minutes\n%d seconds", year, month, day, hour, minute, sec);当我将日期选择器设置为过去1年零1天的日期时,我得到:1年1个月1天16小时0分钟20秒这是1个月零16个小时的时间。如果我将日期选择器设置为过去的1天,我的退税额也一样。更新:我有一个应用程序,根据您的生日(通过UIDatePicker设置),以年为单位计算您的年龄,但是它经常关闭。这证明存在不准确之处,但我不知道它来自哪里,可以吗?
查看完整描述

3 回答

?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

简要描述;简介


这是完成JBRWilkinson答案的另一种方法,但添加了一些代码。它还可以为Alex Reynolds的评论提供解决方案。


使用NSCalendar方法:


(NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts


“使用指定的组件作为两个NSDateComponents对象返回两个提供的日期之间的差”。(来自API文档)。


创建2个NSDate,其差异是要细分的NSTimeInterval。(如果您的NSTimeInterval来自比较2个NSDate,则无需执行此步骤,甚至不需要NSTimeInterval,只需将日期应用于NSCalendar方法即可)。


从NSDateComponents获取报价


样例代码


// The time interval 

NSTimeInterval theTimeInterval = ...;


// Get the system calendar

NSCalendar *sysCalendar = [NSCalendar currentCalendar];


// Create the NSDates

NSDate *date1 = [[NSDate alloc] init];

NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1]; 


// Get conversion to months, days, hours, minutes

NSCalendarUnit unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;


NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];

NSLog(@"Break down: %i min : %i hours : %i days : %i months", [breakdownInfo minute], [breakdownInfo hour], [breakdownInfo day], [breakdownInfo month]);


查看完整回答
反对 回复 2019-12-20
?
catspeake

TA贡献1111条经验 获得超0个赞

这段代码知道夏令时和其他可能令人讨厌的事情。


NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [gregorianCalendar components: (NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit )

                                                    fromDate:startDate

                                                      toDate:[NSDate date]

                                                     options:0];



NSLog(@"%ld", [components year]);

NSLog(@"%ld", [components month]);

NSLog(@"%ld", [components day]);

NSLog(@"%ld", [components hour]);

NSLog(@"%ld", [components minute]);

NSLog(@"%ld", [components second]);


查看完整回答
反对 回复 2019-12-20
?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

这对我有用:


    float *lenghInSeconds = 2345.234513;

    NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:lenghInSeconds];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];



    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];


    [formatter setDateFormat:@"HH:mm:ss"];

    NSLog(@"%@", [formatter stringFromDate:date]); 

    [formatter release];

这里的主要区别是您需要针对时区进行调整。


查看完整回答
反对 回复 2019-12-20
  • 3 回答
  • 0 关注
  • 747 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信