NSTimeInterval to HH:mm:ss? [英] NSTimeInterval to HH:mm:ss?
问题描述
如果我有一个NSTimeInterval设置为200.0,有没有办法将它转换为00:03:20,我想我可以初始化一个NSDate它,然后使用NSDateFormatter使用HH:mm:ss。我的问题是,有一个快速的方法来做这个或者我必须自己分解号码,并使用 [NSString stringWithFormat:%02d:%02d:%02d,myHour,myMin,mySec] / code> c> 不需要使用
NSDateFormatter
?
If I have an NSTimeInterval that is set to say 200.0, is there a way to convert that into 00:03:20, I was thinking I could initialise an NSDate with it and then use NSDateFormatter using HH:mm:ss. My question is, is there a quick way to do this or do I have to break up the number myself and use [NSString stringWithFormat: %02d:%02d:%02d, myHour, myMin, mySec]
?
推荐答案
或除除和模以外的任何东西。 NSTimeInterval
只是含有秒的双精度。
No need to use NSDateFormatter
or anything else than division and modulo. NSTimeInterval
is just a double containing seconds.
Swift
$ b
Swift
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = (interval / 3600)
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
Objective-C
- (NSString *)stringFromTimeInterval:(NSTimeInterval)interval {
NSInteger ti = (NSInteger)interval;
NSInteger seconds = ti % 60;
NSInteger minutes = (ti / 60) % 60;
NSInteger hours = (ti / 3600);
return [NSString stringWithFormat:@"%02ld:%02ld:%02ld", (long)hours, (long)minutes, (long)seconds];
}
这篇关于NSTimeInterval to HH:mm:ss?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!