ios NSTimeInterval 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8128083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSTimeInterval Formatting
提问by Baub
I want to take my NSTimeInterval
and format it into a string as 00:00:00 (hours, minutes, seconds). What is the best way to do this?
我想将我的NSTimeInterval
并将其格式化为 00:00:00(小时、分钟、秒)的字符串。做这个的最好方式是什么?
采纳答案by Michael Frederick
NSTimeInterval interval = ...;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"hh:mm:ss %@", formattedDate);
回答by Johan Kool
Since iOS 8.0 there is now NSDateComponentsFormatter
which has a stringFromTimeInterval:
method.
从 iOS 8.0 开始,现在NSDateComponentsFormatter
有一个stringFromTimeInterval:
方法。
[[NSDateComponentsFormatter new] stringFromTimeInterval:timeInterval];
回答by rob mayoff
"Best" is subjective. The simplest way is this:
“最好”是主观的。最简单的方法是这样的:
unsigned int seconds = (unsigned int)round(myTimeInterval);
NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
seconds / 3600, (seconds / 60) % 60, seconds % 60];
UPDATE
更新
As of iOS 8.0 and Mac OS X 10.10 (Yosemite), you can use NSDateComponentsFormatter
if you need a locale-compliant solution. Example:
从 iOS 8.0 和 Mac OS X 10.10 (Yosemite) 开始,NSDateComponentsFormatter
如果您需要符合区域设置的解决方案,您可以使用。例子:
NSTimeInterval interval = 1234.56;
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute |
NSCalendarUnitSecond;
formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
NSString *string = [formatter stringFromTimeInterval:interval];
NSLog(@"%@", string);
// output: 0:20:34
However, I don't see a way to force it to output two digits for the hour, so if that's important to you, you'll need to use a different solution.
但是,我没有看到强制它在小时内输出两位数的方法,因此如果这对您很重要,您将需要使用不同的解决方案。
回答by William Hu
swift 4.2
快速 4.2
extension Date {
static func timestampString(timeInterval: TimeInterval) -> String? {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.zeroFormattingBehavior = .pad
formatter.maximumUnitCount = 0
formatter.allowedUnits = [.hour, .minute, .second]
return formatter.string(from: timeInterval)
}
}
Test code:
测试代码:
let hour = 60 * 50 * 32
Date.timestampString(timeInterval: TimeInterval(hour))
// output "26:40:00"
Change unitStyle
to get different styles. like formatter.unitsStyle = .abbreviated
get
改变unitStyle
以获得不同的风格。喜欢formatter.unitsStyle = .abbreviated
得到
output: "26h 40m 0s"
输出: "26h 40m 0s"
回答by richdcs
A Swift version of @Michael Frederick's answer :
@Michael Frederick 答案的 Swift 版本:
let duration: NSTimeInterval = ...
let durationDate = NSDate(timeIntervalSince1970: duration)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let durationString = dateFormatter.stringFromDate(durationDate)