在Swift中将计时器标签格式化为小时:分钟:秒

时间:2021-01-24 02:48:01

I have an NSTimer which counts DOWN from 2 hours until 0.

我有一个NSTimer,它从2小时到0来计算DOWN。

Here are some of my code:

以下是我的一些代码:

var timer = NSTimer()
let timeInterval:NSTimeInterval = 0.5
let timerEnd:NSTimeInterval = 0.0
var timeCount:NSTimeInterval = 7200.0 // seconds or 2 hours

// TimeString Function

func timeString(time:NSTimeInterval) -> String {
    let minutes = Int(time) / 60
    let seconds = time - Double(minutes) * 60
    let secondsFraction = seconds - Double(Int(seconds))
    return String(format:"%02i:%02i.%01i",minutes,Int(seconds),Int(secondsFraction * 10.0))
}

The Timer Label is:

计时器标签是:

TimerLabel.text = "Time: \(timeString(timeCount))"

HOWEVER, my timer label shows as:

但是,我的计时器标签显示为:

Time: 200:59.0

How do I format my timer label to look like this:

如何将我的计时器标签格式化为:

Time: 01:59:59 // (which is hours:minutes:seconds)?

[Please note that I have no problems with my countdown timer, I only need to know how to CHANGE THE TIME FORMAT using the TimeString function.]

[请注意我的倒数计时器没有问题,我只需要知道如何使用TimeString函数更改时间格式。]

EDIT: Someone mentioned that my question is a possible duplicate of this one: Swift - iOS - Dates and times in different format. HOWEVER, I am asking on how do I change the time format using the TimeString function that I gave above. I am not asking for another WAY on how to do it.

编辑:有人提到我的问题可能与此相关:Swift - iOS - 日期和时间格式不同。但是,我问我如何使用上面给出的TimeString函数更改时间格式。我不是要求另外一种方法来做到这一点。

For instance:

例如:

let minutes = Int(time) / 60

gives me "200" minutes. etc.

给了我“200”分钟。等等

2 个解决方案

#1


55  

Your calculations are all wrong.

你的计算都错了。

let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)

#2


22  

@rmaddy's solution is accurate and answers the question. However, neither the question nor the solution take into account international users. I suggest using DateComponentsFormatter and let the framework handle the calculations and formatting. Doing so makes your code less error prone and more future proof.

@ rmaddy的解决方案是准确的,并回答了这个问题。但是,问题和解决方案都没有考虑到国际用户。我建议使用DateComponentsFormatter并让框架处理计算和格式化。这样做可以使您的代码更容易出错,并且可以进一步证明未

I came across this blog post that provides a concise solution: http://crunchybagel.com/formatting-a-duration-with-nsdatecomponentsformatter/

我看到这篇博文提供了一个简洁的解决方案:http://crunchybagel.com/formatting-a-duration-with-nsdatecomponentsformatter/

Pulled from that post, this is the code snippet that would replace the code you're currently using to make your calculations. Updated for Swift 3:

从该帖子中拉出,这是代码片段,它将替换您当前用于进行计算的代码。针对Swift 3进行了更新:

let duration: TimeInterval = 7200.0

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
formatter.allowedUnits = [ .hour, .minute, .second ] // Units to display in the formatted string
formatter.zeroFormattingBehavior = [ .pad ] // Pad with zeroes where appropriate for the locale

let formattedDuration = formatter.string(from: duration) 

#1


55  

Your calculations are all wrong.

你的计算都错了。

let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)

#2


22  

@rmaddy's solution is accurate and answers the question. However, neither the question nor the solution take into account international users. I suggest using DateComponentsFormatter and let the framework handle the calculations and formatting. Doing so makes your code less error prone and more future proof.

@ rmaddy的解决方案是准确的,并回答了这个问题。但是,问题和解决方案都没有考虑到国际用户。我建议使用DateComponentsFormatter并让框架处理计算和格式化。这样做可以使您的代码更容易出错,并且可以进一步证明未

I came across this blog post that provides a concise solution: http://crunchybagel.com/formatting-a-duration-with-nsdatecomponentsformatter/

我看到这篇博文提供了一个简洁的解决方案:http://crunchybagel.com/formatting-a-duration-with-nsdatecomponentsformatter/

Pulled from that post, this is the code snippet that would replace the code you're currently using to make your calculations. Updated for Swift 3:

从该帖子中拉出,这是代码片段,它将替换您当前用于进行计算的代码。针对Swift 3进行了更新:

let duration: TimeInterval = 7200.0

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
formatter.allowedUnits = [ .hour, .minute, .second ] // Units to display in the formatted string
formatter.zeroFormattingBehavior = [ .pad ] // Pad with zeroes where appropriate for the locale

let formattedDuration = formatter.string(from: duration)