我做的是:
How to add minutes to current time in swift
how to add 30 minutes to current time
这是我的代码:
endFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let endTimeString = "2017-01-16 12:58:56"
let endTime = endFormatter.dateFromString(endTimeString)
endTime?.dateByAddingTimeInterval(180) // 3 Minute
print(endTime)
第一个回答@rob 我尝试了那个但失败了。第二个答案建议 dateByAddingTimeInterval
我不介意它是否有效。
最佳答案
您需要像@Rob 那样使用dateByAddingUnit
而不是dateByAddingTimeInterval
。您没有获得正确时间的原因可能是 TimeZone
,因此请尝试使用您的 NSDateFormatter
实例设置 timeZone
。
endFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
endFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
let endTimeString = "2017-01-16 12:58:56"
let endTime = endFormatter.dateFromString(endTimeString)
//Now add the 3 minute in endTime
let calendar = NSCalendar.currentCalendar()
let date = calendar.dateByAddingUnit(.Minute, value: 3, toDate: endTime, options: [])
关于ios - 如何在swift2中将分钟添加到自定义时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41675224/