我一直在寻找,但找不到解决我的问题的确切解决方案。我一直在尝试从约会中获取 1 小时前的信息。我怎样才能快速实现这一目标?
最佳答案
为了正确计算涉及 NSDate 并考虑不同日历的所有边缘情况(例如,在夏令时之间切换),您应该使用 NSCalendar 类:
Swift 3+
let earlyDate = Calendar.current.date(
byAdding: .hour,
value: -1,
to: Date())
较旧
// Get the date that was 1hr before now
let earlyDate = NSCalendar.currentCalendar().dateByAddingUnit(
.Hour,
value: -1,
toDate: NSDate(),
options: [])
关于ios - 如何在 iOS swift 中获取 1 小时前的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41903340/