How to get 1 hour ago from a date in iOS swift?

前端 未结 13 771
一向
一向 2020-12-04 13:35

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?

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 14:02

    for Swift 2:

    extension NSDate {
        func after(value: Int, calendarUnit:NSCalendarUnit) -> NSDate {
            return calendar.dateByAddingUnit(calendarUnit, value: value, toDate: self, options: [])!
        }
    }
    

    how to use:

    let lastHour = NSDate().after(-1, calendarUnit: .Hour)
    

提交回复
热议问题