我看到了这个答案:Get current NSDate in timestamp format
但出于某种原因,它给了我错误。
我需要的是能够指定从现在到接下来 2 分钟之间的时间戳,或者从最后 2 分钟到现在之间的时间间隔。
例如:interval => now-2minutes to now(前2分钟到现在的时间)
谢谢。
最佳答案
如果我做对了,这应该可以为您澄清一些事情:
let calendar = NSCalendar.currentCalendar()
let now = NSDate()
let comps = NSDateComponents()
comps.minute = 2
let twoMinutesFromNow = calendar.dateByAddingComponents(comps, toDate: now, options: NSCalendarOptions.MatchStrictly)
let interval = twoMinutesFromNow?.timeIntervalSinceDate(now)
Here是一篇关于使用 NSCalendar
和 NSDateComponents
NSDate
的好文章
关于ios - 如何快速获取时间戳间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30666982/