objective-c 将秒整数转换为 HH:MM, iPhone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1739383/
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
Convert Seconds Integer To HH:MM, iPhone
提问by Stumf
I am struggling with this. I have a value in seconds that I want to display in a label in HH:MM format. I have searched the internet for ages and found some answers, but either not fully understood them, or they seem like an odd way of doing what I want. If someone could help me out on this one that would be great! Bear in mind that I am new to this games so this question may seem like a really basic one to the more experienced out there.
我正在为此而苦苦挣扎。我有一个以秒为单位的值,我想以 HH:MM 格式显示在标签中。我已经在互联网上搜索了很长时间并找到了一些答案,但要么没有完全理解它们,要么它们看起来像是做我想做的事情的奇怪方式。如果有人能帮我解决这个问题,那就太好了!请记住,我是这个游戏的新手,所以这个问题对于更有经验的人来说似乎是一个非常基本的问题。
回答by Rohit Agarwal
I was looking for the same thing that you are looking but couldn't find one. So I wrote one -
我正在寻找与您正在寻找的相同的东西,但找不到。于是我写了一篇——
- (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];
}
works perfectly in Swiftas well:
在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)
}
回答by edukulele
In iOS 8.0 and higher versions it can also be done with NSDateComponentsFormatter. I need to mention that it will format the string without first leading zero, for example '9:30', but not '09:30'. But if you like to use formatters, you can use this code:
在 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];
}
回答by Mongi Zaidi
This is how I do it:
这就是我的做法:
-(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];
}
回答by Aks
For 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)"
}
}
回答by Isuru
While @Aks' answerfor Swift works, I'm a little skeptic about hardcoding values in my code. @edukulele's answeris much more cleaner but it's in Objective-C. I translated it to Swift with a slight change. I normally write my formatters as lazy vars.
虽然@Aks对 Swift的回答有效,但我对代码中的硬编码值有点怀疑。@edukulele 的答案更简洁,但它在 Objective-C 中。我将它翻译成 Swift,稍作改动。我通常将格式化程序写为lazy vars。
private lazy var dateFormatter: NSDateComponentsFormatter = {
let formatter = NSDateComponentsFormatter()
formatter.zeroFormattingBehavior = .Pad
formatter.allowedUnits = [.Hour, .Minute, .Second]
formatter.unitsStyle = .Positional
return formatter
}()
回答by Perry C
For Swift with clock count
对于带有时钟计数的 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)"
}
}

