参考:http://stackoverflow.com/questions/11121459/how-to-convert-nstimeinterval-to-int
- 简单粗暴法:
let interval: TimeInterval = NSDate.timeIntervalSinceReferenceDate;
print("interval: \(interval)")
let time = Int(interval)
print("time: \(time)")
//结果为:interval: 496138595.969168
//time: 496138595
这样是直接截断,不太友好。
- 四舍五入法:
可精确到秒或毫秒:
let aInterval = 496138080.543964
//精确到秒
let second = Int(round(aInterval))
//精确到毫秒
let millisecond = CLongLong(round(aInterval*1000))
print("second: \(second), millisecond: \(millisecond)")
//结果为:second: 496138081, millisecond: 496138080544
网友评论