如何让 NSTimer 显示月:周:天
How to make NSTimer show Month : Weeks : Days
我正在尝试让计时器显示以下内容 - 年:月:周:日:时:分:秒:毫秒。
我不希望它在秒数达到 60 之前显示分钟,小时数天等也是如此。例如:如果它保持在 3 天 4 小时 39 分 3 秒 43 毫秒,它应该' 显示月份或年份,因为到目前为止它们都是 0。
这是我的。
@IBOutlet weak var timeLabel: UILabel!
var timerCount = 0
var timerRuning = false
var timer = NSTimer()
func counting() {
timerCount++
timeLabel.text = "\(timerCount)"
}
@IBAction func startTime(sender: UIButton)
{
if timerRuning == false {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: ("counting"), userInfo: nil, repeats: true)
timerRuning = true
}
}
@IBAction func finishTime(sender: UIButton)
{
if timerRuning == true {
timer.invalidate()
timerRuning = false
}
}
问题是它只显示秒。我怎样才能让它显示所有其余部分? (就像我上面写的那样。)
iOS 8.0 中引入了一个新的 class NSDateComponentsFormatter
,它可以将秒数格式化为可读的时间字符串。
func counting() {
timerCount++
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Full
timeLabel.text = formatter.stringFromTimeInterval(NSTimeInterval(timerCount))
}
但有一个限制:它不考虑小于秒的单位。
我正在尝试让计时器显示以下内容 - 年:月:周:日:时:分:秒:毫秒。
我不希望它在秒数达到 60 之前显示分钟,小时数天等也是如此。例如:如果它保持在 3 天 4 小时 39 分 3 秒 43 毫秒,它应该' 显示月份或年份,因为到目前为止它们都是 0。
这是我的。
@IBOutlet weak var timeLabel: UILabel!
var timerCount = 0
var timerRuning = false
var timer = NSTimer()
func counting() {
timerCount++
timeLabel.text = "\(timerCount)"
}
@IBAction func startTime(sender: UIButton)
{
if timerRuning == false {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: ("counting"), userInfo: nil, repeats: true)
timerRuning = true
}
}
@IBAction func finishTime(sender: UIButton)
{
if timerRuning == true {
timer.invalidate()
timerRuning = false
}
}
问题是它只显示秒。我怎样才能让它显示所有其余部分? (就像我上面写的那样。)
iOS 8.0 中引入了一个新的 class NSDateComponentsFormatter
,它可以将秒数格式化为可读的时间字符串。
func counting() {
timerCount++
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Full
timeLabel.text = formatter.stringFromTimeInterval(NSTimeInterval(timerCount))
}
但有一个限制:它不考虑小于秒的单位。