如何从iOS swift中的约会前1小时前获得?
问题描述:
我一直在研究,但我无法找到解决问题的确切方法。我一直试图从一个约会前1小时前得到。我怎样才能在swift中实现这一目标?
I have been researching, but I couldnt find exact solution for my problem. I have been trying to get 1 hour ago from a date. How can I achieve this in swift?
答
对于涉及NSDate的正确计算,它考虑了不同日历的所有边缘情况(例如在节省时间之间切换)你应该使用NSCalendar类:
For correct calculations involving NSDate that take into account all edge cases of different calendars (e.g. switching between day saving time) you should use NSCalendar class:
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: [])