NSTimeInterval 格式化

发布于 2024-12-15 15:37:06 字数 79 浏览 3 评论 0原文

我想将我的 NSTimeInterval 并将其格式化为字符串 00:00:00(小时、分钟、秒)。最好的方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情域 2024-12-22 15:37:06

从 iOS 8.0 开始,现在有了 NSDateComponentsFormatter,它有一个 stringFromTimeInterval: 方法。

[[NSDateComponentsFormatter new] stringFromTimeInterval:timeInterval];

Since iOS 8.0 there is now NSDateComponentsFormatter which has a stringFromTimeInterval: method.

[[NSDateComponentsFormatter new] stringFromTimeInterval:timeInterval];
做个少女永远怀春 2024-12-22 15:37:06

“最好”是主观的。最简单的方法是这样的:

unsigned int seconds = (unsigned int)round(myTimeInterval);
NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
    seconds / 3600, (seconds / 60) % 60, seconds % 60];

更新

从 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

但是,我没有找到强制其输出小时两位数的方法,因此如果这对您很重要,您将需要使用不同的解决方案。

"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:

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.

北城孤痞 2024-12-22 15:37:06
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);
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);
梦巷 2024-12-22 15:37:06

swift 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)
    }
}

测试代码:

let hour = 60 * 50 * 32
Date.timestampString(timeInterval: TimeInterval(hour))

// output "26:40:00"

更改 unitStyle 以获得不同的样式。像formatter.unitsStyle = .abbreviated获取

输出:“26h 40m 0s”

swift 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

output: "26h 40m 0s"

断肠人 2024-12-22 15:37:06

@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)

A Swift version of @Michael Frederick's answer :

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文