SWIFT: How do I add hours to NSDate object

后端 未结 4 1768
逝去的感伤
逝去的感伤 2021-01-02 18:18

I generate a NSDate object from string.

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = \"yyyy-MM-dd HH:mm:ss\"
dateFormatter.timeZone = NS         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 19:00

    For Swift 3 you can use this function:

    //get next date by adding hours func  
    
    getNewDateAfterAddingHours(hoursToAdd:NSInteger, oldDate:Date) -> Int64 {
        let calendar = Calendar.current
        let newDate = calendar.date(byAdding: .hour, value: hoursToAdd, to: oldDate)
        return Int64((newDate?.timeIntervalSince1970)!)
    }
    

提交回复
热议问题