NSTimeInterval到HH:mm:ss?

如果我有一个NSTimeInterval设置为200.0,有没有办法将其转换为00:03:20,我想我可以初始化一个NSDate,然后使用NSHateTimematter使用HH:mm:ss。 我的问题是,是否有一个快速的方法来做到这一点,或者我必须打破自己的数字和使用[NSString stringWithFormat: %02d:%02d:%02d, myHour, myMin, mySec]

没有必要使用NSDateFormatter或除了除法和模数。 NSTimeInterval只是一个包含秒的双NSTimeInterval

迅速

 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]; } 

在iOS 8上,使用NSDateComponentsFormatter

 NSDateComponentsFormatter *dateComponentsFormatter = [[NSDateComponentsFormatter alloc] init]; NSLog(@"%@", [dateComponentsFormatter stringFromTimeInterval:200.0]); 

输出“3:20”。

 NSDateComponentsFormatter *dateComponentsFormatter = [[NSDateComponentsFormatter alloc] init]; dateComponentsFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad; dateComponentsFormatter.allowedUnits = (NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond); NSLog(@"%@", [dateComponentsFormatter stringFromTimeInterval:200.0]); 

输出“0:03:20”。

如果你的目标是iOS 7以上,Roshit的答案是好的。 我在这里重写它来修复一些编译器警告等:

 NSString *FormattedStringFromTimeInterval(NSTimeInterval timeInterval) { // https://lists.apple.com/archives/Cocoa-dev/2006/Feb/msg01186.html // http://stackoverflow.com/questions/4933075/nstimeinterval-to-hhmmss NSDate *endingDate = [NSDate date]; NSDate *startingDate = [endingDate dateByAddingTimeInterval:-timeInterval]; NSCalendarUnit components = (NSCalendarUnit)(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit); NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:components fromDate:startingDate toDate:endingDate options:(NSCalendarOptions)0]; return [NSString stringWithFormat:@"%ld:%ld:%ld", (long)dateComponents.hour, (long)dateComponents.minute, (long)dateComponents.second]; } 

一些额外的代码行,但我觉得使用NSDateComponents会给出更精确的值。

 - (NSString *)getTimeRepresentationFromDate:(NSDate *)iDate withTimeInterval:(NSTimeInterval)iTimeInterval { NSString *aReturnValue = nil; NSDate *aNewDate = [iDate dateByAddingTimeInterval:iTimeInterval]; unsigned int theUnits = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSCalendar *aCalender = [NSCalendar currentCalendar]; NSDateComponents *aDateComponents = [aCalender components:theUnits fromDate:iDate toDate:aNewDate options:0]; aReturnValue = [NSString stringWithFormat:@"%d:%d:%d", [aDateComponents hour], [aDateComponents minute], [aDateComponents second]]; return aReturnValue; } 

swift 3版本的onmyway133的回答:

 import Foundation func format(_ duration: TimeInterval) -> String { let formatter = DateComponentsFormatter() formatter.zeroFormattingBehavior = .pad formatter.allowedUnits = [.minute, .second] if duration >= 3600 { formatter.allowedUnits.insert(.hour) } return formatter.string(from: duration)! } print(format(12)) // 0:12 print(format(65)) // 1:05 print(format(1750)) // 29:10 print(format(3890)) // 1:04:50 print(format(45720)) // 12:42:00 
 NSTimeInterval ti = 3667; double hours = floor(ti / 60 / 60); double minutes = floor((ti - (hours * 60 * 60)) / 60); double seconds = floor(ti - (hours * 60 * 60) - (minutes * 60)); 

为了“扩展”Matthias Bauch的build议,在Swift中,我将这个计算属性定义为NSTimeInterval:

 extension NSTimeInterval { var stringValue: String { let interval = Int(self) let seconds = interval % 60 let minutes = (interval / 60) % 60 let hours = (interval / 3600) return String(format: "%02d:%02d:%02d", hours, minutes, seconds) } } 

这样做的好处是附加到NSTimeIntervaltypes,而不是你的视图控制器,或者你把它放在哪里。 使用你会像这样的东西:

 let timeInterval = NSDate().timeIntervalSinceDate(start) self.elapsedTimeLabel.text = timeInterval.stringValue 

Swift 2中iOS 8+ 。 这确保我们只在必要时才显示小时

 func format(duration: NSTimeInterval) -> String { let formatter = NSDateComponentsFormatter() formatter.zeroFormattingBehavior = .Pad if duration >= 3600 { formatter.allowedUnits = [.Hour, .Minute, .Second] } else { formatter.allowedUnits = [.Minute, .Second] } return formatter.stringFromTimeInterval(duration) ?? "" } 

所以你有了

 print(format(12)) // 0:12 print(format(65)) // 1:05 print(format(1750)) // 29:10 print(format(3890)) // 1:04:50 print(format(45720)) // 12:42:00