我为此感到挣扎。我有一个以秒为单位的值,该值想以HH:MM格式显示在标签中。我已经在互联网上搜索了很长时间,并找到了一些答案,但是要么没有完全理解它们,要么它们似乎是做我想要的事情的一种奇怪方法。如果有人可以帮我解决这个问题,那就太好了!请记住,我是这个游戏的新手,所以对于那里的经验丰富的人来说,这个问题似乎是一个非常基本的问题。
Answers:
我一直在寻找与您正在寻找的东西相同的东西,但找不到。所以我写了一个-
- (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)
}
minutes = totalSeconds / 60
。不要太复杂。
在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];
}
NSCalendarUnitSecond
单位添加到allowedUnits中。此示例也适用于Mac OS X应用程序。
这是我的方法:
-(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];
}
对于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)"
}
}
@Aks对Swift的回答有效时,我对代码中的硬编码值有些怀疑。@edukulele的答案要干净得多,但是在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
}()
H:MM
,不是HH:MM
要求的。
对于带有时钟计数的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)"
}
}