ios - 如何快速获取时间戳间隔?

标签 ios swift timestamp

我看到了这个答案: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是一篇关于使用 NSCalendarNSDateComponents

处理 NSDate 的好文章

关于ios - 如何快速获取时间戳间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30666982/

相关文章:

iphone - 检测iphone方向的最佳方法

ios - 无法在 CoreAnimation 完成后更新模型层

iOS/GLES2 : How to programmatically determine maximum available texture units?

swift - 在 Alertview 中快速创建和自定义 UITextfield

python - 如果时间戳 20160322T192202 不以 'Z' 结尾,那么 T 意味着什么?

perl - 在 Perl 中将 Unix 时间戳转换为可读日期

php - 使用 php、pdo 和 mysql 显示当前登录用户的简单计数器

ios - 不区分大小写的数组搜索

ios - 通过GStreamer将文件保存到iOS目录中

Swift:将包含自定义标签的单元格添加到 UITableView