如果我的NSTimeInterval设置为200.0,是否有办法将其转换为00:03:20,我想我可以用它初始化NSDate,然后使用HH:mm:ss使用NSDateFormatter。我的问题是,有没有一种快速的方法来执行此操作,或者我必须自己分解并使用数字[NSString stringWithFormat: %02d:%02d:%02d, myHour, myMin, mySec]
?
Answers:
无需使用NSDateFormatter
除除法和取模以外的任何方法。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)
}
物镜
- (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];
}
NSDateComponentsFormatter().stringFromTimeInterval(NSTimeInterval(duration))
快速
在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”。
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
zeroFormattingBehavior
为[.dropLeading, .pad]
一个更好的(较少代码)选项。
一些额外的代码行,但是我觉得使用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 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
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的建议,在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)
}
}
这样做的好处是它附加在NSTimeInterval
类型上,而不是视图控制器或附加在该函数上的任何其他位置。要使用它,您需要执行以下操作:
let timeInterval = NSDate().timeIntervalSinceDate(start)
self.elapsedTimeLabel.text = timeInterval.stringValue
根据@ onmyway133的回答,这是Swift 4版本:
func format(duration: TimeInterval) -> String {
let formatter = DateComponentsFormatter()
formatter.zeroFormattingBehavior = .pad
if duration >= 3600 {
formatter.allowedUnits = [.hour, .minute, .second];
} else {
formatter.allowedUnits = [.minute, .second];
}
return formatter.string(from: duration) ?? "";
}
直接来自苹果文档:在h文件中:
@property(strong,nonatomic)NSDateComponentsFormatter *timerFormatter;
在m文件中
@synthesize timerFormatter;
- (void)viewDidLoad {
[super viewDidLoad];
NSDateComponentsFormatter *timerFormatter = [[NSDateComponentsFormatter alloc] init];
timerFormatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
//10:59 Positional THIS ONE DOES NOT SEEM TO WORK iOS<9 WHEN <1Minute JUST SHOWS 01, 10m 59s Abbreviated, 10min 59sec Short, 10minutes 59seconds Full ...
timerFormatter.allowedUnits = NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
}
在需要将NSTimeInterval timeInterval转换为hh:mm:ss字符串的任何地方,请执行以下操作:
NSString *txt=[timerFormatter stringFromTimeInterval:timeInterval];
我想应该将计时器分数淘汰。当Matthias的代码在几秒钟内创建该问题时,我使用了与Matthias稍有不同的以下内容
- (NSString *)stringFromTimeInterval:(NSTimeInterval)interval {
int ti = (int) ceil(interval);
int seconds = ti % 60;
int minutes = (ti / 60) % 60;
int hours = (ti / 3600);
return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];
}
Objective C版本的onmyway133的答案
- (NSString*) formatTimeInterval:(NSTimeInterval) timeInterval {
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
if (timeInterval > 3600) {
formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
} else {
formatter.allowedUnits = NSCalendarUnitMinute | NSCalendarUnitSecond;
}
return [formatter stringFromTimeInterval:timeInterval];
}