如何在 iPhone 上将 NSTimeInterval 分解为年、月、日、小时、分钟和秒?
我有一个跨越数年的时间间隔,我想要从年到秒的所有时间分量。
我的第一个想法是将时间间隔整数除以一年中的秒数,从运行总秒数中减去该值,将其除以一个月内的秒数,从运行总数中减去该值,依此类推。
这看起来很复杂,我读过,每当你做一些看起来很复杂的事情时,可能有一个内置的方法。
有没有?
我将亚历克斯的第二种方法集成到我的代码中。
它位于我的界面中 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 设置)计算您的年龄(以岁为单位),但它经常关闭。 这证明有一个不准确的地方,但我不知道它来自哪里,你能吗?
I have a time interval that spans years and I want all the time components from year down to seconds.
My first thought is to integer divide the time interval by seconds in a year, subtract that from a running total of seconds, divide that by seconds in a month, subtract that from the running total and so on.
That just seems convoluted and I've read that whenever you are doing something that looks convoluted, there is probably a built-in method.
Is there?
I integrated Alex's 2nd method into my code.
It's in a method called by a UIDatePicker in my interface.
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);
When I set the date picker to a date 1 year and 1 day in the past, I get:
1 years 1 months 1 days 16 hours 0
minutes 20 seconds
which is 1 month and 16 hours off. If I set the date picker to 1 day in the past, I am off by the same amount.
Update: I have an app that calculates your age in years, given your birthday (set from a UIDatePicker), yet it was often off. This proves there was an inaccuracy, but I can't figure out where it comes from, can you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
简要说明
这只是完成 JBRWilkinson 答案的另一种方法,但添加了一些代码。 它还可以为亚历克斯·雷诺兹的评论提供解决方案。
使用 NSCalendar 方法:
(NSDateComponents *)组件:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate 选项:(NSUInteger)opts
“作为使用指定组件的 NSDateComponents 对象,返回两个提供的日期之间的差异”。 (来自 API 文档)。
创建2个NSDate,其差异是你想要分解的NSTimeInterval。 (如果您的 NSTimeInterval 来自比较 2 个 NSDate,则不需要执行此步骤,甚至不需要 NSTimeInterval,只需将日期应用到 NSCalendar 方法即可)。
从 NSDateComponents 获取您的报价
示例代码
Brief Description
Just another approach to complete the answer of JBRWilkinson but adding some code. It can also offers a solution to Alex Reynolds's comment.
Use NSCalendar method:
(NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts
"Returns, as an NSDateComponents object using specified components, the difference between two supplied dates". (From the API documentation).
Create 2 NSDate whose difference is the NSTimeInterval you want to break down. (If your NSTimeInterval comes from comparing 2 NSDate you don't need to do this step, and you don't even need the NSTimeInterval, just apply the dates to the NSCalendar method).
Get your quotes from NSDateComponents
Sample Code
这段代码知道夏令时和其他可能的讨厌的事情。
This code is aware of day light saving times and other possible nasty things.
从 iOS8 及更高版本,您可以使用
NSDateComponentsFormatter
它具有将时差转换为用户友好格式字符串的方法。
输出为 - 2 周、4 天、18 小时、57 分钟、32 秒
From iOS8 and above you can use
NSDateComponentsFormatter
It has methods to convert time difference in user friendly formatted string.
This gives the output - 2 weeks, 4 days, 18 hours, 57 minutes, 32 seconds
使用
+dateWithIntervalSince1970
将间隔转换为NSDate
,使用NSCalendar
的-componentsFromDate
从中获取日期组件> 方法。SDK 参考
Convert your interval into an
NSDate
using+dateWithIntervalSince1970
, get the date components out of that usingNSCalendar
's-componentsFromDate
method.SDK Reference
这对我有用:
这里的主要区别是您需要调整时区。
This works for me:
The main difference here is that you need to adjust for the timezone.
或者有我的类方法。 它不处理年,但可以很容易地添加,尽管它更适合天、小时和分钟等小时间间隔。 它考虑了复数并且仅显示需要的内容:
Or there is my class method. It doesn't handle years, but that could easily be addedn though it's better for small timelaps like days, hours and minutes. It take plurals into account and only shows what's needed:
这是另一种可能,更清晰一些:
Here's another possibility, somewhat cleaner: