我正在努力解决这个问题。我有一个以秒为单位的值,我希望以HH:MM格式显示在标签中。我已经在互联网上搜索了很多年,并找到了一些答案,但要么没有完全理解它们,要么它们似乎是一种做我想做的奇怪方式。如果有人可以帮我解决这个问题,那就太棒了!请记住,我是这个游戏的新手,所以这个问题对于那些经验丰富的人来说似乎是一个非常基本的问题。
答案 0 :(得分:250)
我正在寻找你正在寻找但却找不到的东西。所以我写了一个 -
- (NSString *)timeFormatted:(int)totalSeconds
{
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];
}
也可以在 Swift 中完美运行:
func timeFormatted(totalSeconds: Int) -> String {
let seconds: Int = totalSeconds % 60
let minutes: Int = (totalSeconds / 60) % 60
let hours: Int = totalSeconds / 3600
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
答案 1 :(得分:32)
在iOS 8.0及更高版本中,也可以使用NSDateComponentsFormatter
完成。我需要提一下,它会格式化字符串,而不是先导致零,例如' 9:30',但不是' 09:30'。但是如果你想使用格式化程序,可以使用以下代码:
-(NSString *)getTimeStringFromSeconds:(double)seconds
{
NSDateComponentsFormatter *dcFormatter = [[NSDateComponentsFormatter alloc] init];
dcFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
dcFormatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute;
dcFormatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
return [dcFormatter stringFromTimeInterval:seconds];
}
答案 2 :(得分:9)
我就是这样做的:
-(NSString *)formatTimeFromSeconds:(int)numberOfSeconds
{
int seconds = numberOfSeconds % 60;
int minutes = (numberOfSeconds / 60) % 60;
int hours = numberOfSeconds / 3600;
//we have >=1 hour => example : 3h:25m
if (hours) {
return [NSString stringWithFormat:@"%dh:%02dm", hours, minutes];
}
//we have 0 hours and >=1 minutes => example : 3m:25s
if (minutes) {
return [NSString stringWithFormat:@"%dm:%02ds", minutes, seconds];
}
//we have only seconds example : 25s
return [NSString stringWithFormat:@"%ds", seconds];
}
答案 3 :(得分:7)
对于Swift:
func formatTimeInSec(totalSeconds: Int) -> String {
let seconds = totalSeconds % 60
let minutes = (totalSeconds / 60) % 60
let hours = totalSeconds / 3600
let strHours = hours > 9 ? String(hours) : "0" + String(hours)
let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
if hours > 0 {
return "\(strHours):\(strMinutes):\(strSeconds)"
}
else {
return "\(strMinutes):\(strSeconds)"
}
}
答案 4 :(得分:6)
虽然@Aks'answer适用于Swift,但我对代码中的硬编码值有点怀疑。 @ edukulele的answer更清洁,但它在Objective-C中。我稍微改了一下,把它翻译成了Swift。我通常将格式化程序写为lazy var
s。
private lazy var dateFormatter: NSDateComponentsFormatter = {
let formatter = NSDateComponentsFormatter()
formatter.zeroFormattingBehavior = .Pad
formatter.allowedUnits = [.Hour, .Minute, .Second]
formatter.unitsStyle = .Positional
return formatter
}()
答案 5 :(得分:0)
对于具有时钟计数的Swift
var timer = NSTimer()
var count = 1
func updateTime() {
count++
let seconds = count % 60
let minutes = (count / 60) % 60
let hours = count / 3600
let strHours = hours > 9 ? String(hours) : "0" + String(hours)
let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
if hours > 0 {
clockOutlet.text = "\(strHours):\(strMinutes):\(strSeconds)"
}
else {
clockOutlet.text = "\(strMinutes):\(strSeconds)"
}
}