How does one subtract hours from an NSDate?

后端 未结 7 1486
长发绾君心
长发绾君心 2020-12-09 14:56

I would like to subtract 4 hours from a date. I read the date string into an NSDate object use the following code:

NSDateFormatter * dateFormatter = [[[NSDat         


        
7条回答
  •  我在风中等你
    2020-12-09 15:47

    Since iOS 8 there is the more convenient dateByAddingUnit:

    Swift 2.x

    //subtract 3 hours
    let calendar = NSCalendar.autoupdatingCurrentCalendar()
    newDate = calendar.dateByAddingUnit(.Hour, value: -3, toDate: originalDate, options: [])
    

提交回复
热议问题