使用 NSDate 格式化 NSTimeInterval
Format NSTimeInterval with NSDate
我在 Swift 中有一个函数输出以下信息:
1970-01-01 00:00:06 +0000
1970-01-01 00:00:07 +0000
等它本质上是一个秒表。我想获取该数字并将其格式化为 00:00:00 ,包含分钟、秒和毫秒。这是我的代码:
func update() {
currentTime = NSDate().timeIntervalSince1970
elapsedTime = currentTime - startDateInterval
var testDate = NSDate(timeIntervalSince1970: elapsedTime)
println(testDate)
}
currentTime
是对每次对 update()
执行 ping 操作(每 .01 秒)
的日期的引用
startDateInterval
是对秒表启动时存储的日期的引用。
如有任何帮助,我们将不胜感激! :)
我猜你要找的是
var dateFormatter = NSDateFormatter()
您可能希望将其格式化为
self.dateFormatter.dateFormat = "mm:ss:SSS"
self.dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
对于输出,您需要在其中插入日期差异,即
let now = NSDate()
let timeintervalTotal = now.timeIntervalSinceDate(self.startDateTotal)
self.labelTotalTime.text = self.dateFormatter.stringFromDate(NSDate(timeIntervalSinceReferenceDate: timeintervalTotal))
我在 Swift 中有一个函数输出以下信息:
1970-01-01 00:00:06 +0000
1970-01-01 00:00:07 +0000
等它本质上是一个秒表。我想获取该数字并将其格式化为 00:00:00 ,包含分钟、秒和毫秒。这是我的代码:
func update() {
currentTime = NSDate().timeIntervalSince1970
elapsedTime = currentTime - startDateInterval
var testDate = NSDate(timeIntervalSince1970: elapsedTime)
println(testDate)
}
currentTime
是对每次对 update()
执行 ping 操作(每 .01 秒)
startDateInterval
是对秒表启动时存储的日期的引用。
如有任何帮助,我们将不胜感激! :)
我猜你要找的是
var dateFormatter = NSDateFormatter()
您可能希望将其格式化为
self.dateFormatter.dateFormat = "mm:ss:SSS"
self.dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
对于输出,您需要在其中插入日期差异,即
let now = NSDate()
let timeintervalTotal = now.timeIntervalSinceDate(self.startDateTotal)
self.labelTotalTime.text = self.dateFormatter.stringFromDate(NSDate(timeIntervalSinceReferenceDate: timeintervalTotal))