NSNumber of seconds to hours, minute, seconds [英] NSNumber of seconds to Hours, minutes, seconds
问题描述
我在尝试做一些应该很容易的事情时感觉很糟糕.我的 NSNumber 值为 32025.89 秒.我需要用小时、分钟、秒来表示.有什么方法可以吐出来吗?我似乎找不到合适的格式化程序.
I am having a terrible time trying to do something that should be easy. I have a NSNumber value of 32025.89 seconds. I need to represent that in Hours, Minutes, Seconds. Is there a method that spits that out? I can't seem to find a proper formatter.
推荐答案
您是否尝试过从中创建 NSDate
并使用 NSDateFormatter
打印它?
Have you tried creating an NSDate
from that and printing that using an NSDateFormatter
?
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[theNumber doubleValue]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
NSLog(@"%@", [formatter stringFromDate:date]);
[formatter release];
如果您需要小时、分钟和秒的单独值,请按照 nall 的建议使用 NSDateComponents
.
If you need the individual values for hours, minutes, and seconds, use NSDateComponents
as suggested by nall.
如果 NSNumber
表示超过一天,上面的将不会打印正确的数量.它可以是 1 天 1 小时,它只会打印 01:00:00.在这种情况下,您应该手动计算小时、分钟和秒.
The above won't print the correct amounts if the NSNumber
represents more than one day. It could be 1 day and 1 hour, and it would only print 01:00:00. In such a case you should calculate hours, minutes and seconds manually.
这篇关于NSNumber of seconds to hours, minute, seconds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!