wil*_*lc2 55 iphone time cocoa nstimeinterval
我有一个跨越多年的时间间隔,我希望所有的时间组件从一年到几秒.
我的第一个想法是将一年中的时间间隔除以整数秒,从运行的总秒数中减去该值,将其除以一个月中的秒数,从运行总计中减去该值,依此类推.
这看起来很复杂,我已经读过,无论何时你做一些看起来错综复杂的事情,都可能有内置的方法.
在那儿?
我将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);
Run Code Online (Sandbox Code Playgroud)
当我将日期选择器设置为过去1年和1天的日期时,我得到:
1年1个月1天16小时0分20秒
这是1个月和16个小时.如果我将日期选择器设置为过去的1天,我的数量相同.
更新:我有一个应用程序,计算你的年龄,给定你的生日(从UIDatePicker设置),但它经常关闭.这证明存在不准确性,但我无法弄清楚它来自哪里,可以吗?
小智 100
简要描述;简介
只是另一种方法来完成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]);
Run Code Online (Sandbox Code Playgroud)
Vik*_*ica 17
这段代码知道白天节省时间和其他可能令人讨厌的东西.
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]);
Run Code Online (Sandbox Code Playgroud)
从iOS8及以上版本,您可以使用 NSDateComponentsFormatter
它有方法来转换用户友好的格式化字符串中的时差.
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
NSLog(@"%@", [formatter stringFromTimeInterval:1623452]);
Run Code Online (Sandbox Code Playgroud)
这给出了输出 - 2周,4天,18小时,57分钟,32秒