[英]How to add minutes to custom time in swift2
What I do is: 我要做的是:
How to add minutes to current time in swift 如何快速将分钟添加到当前时间
how to add 30 minutes to current time 如何在当前时间增加30分钟
Here is my code: 这是我的代码:
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)
1st answer @rob i tried that one but failed . 第一个答案@rob我尝试了一个,但是失败了。 second answer suggest
dateByAddingTimeInterval
i am not suer is it is work or not . 第二个答案建议
dateByAddingTimeInterval
我不知道这是工作还是没有。
You need to use dateByAddingUnit
as @Rob doing instead of dateByAddingTimeInterval
. 您需要使用
dateByAddingUnit
作为@Rob来代替dateByAddingTimeInterval
。 The reason you are not getting correct time is may because of TimeZone
so try to set timeZone
with your NSDateFormatter
instance. 您未获得正确时间的原因可能是由于
TimeZone
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: [])
let dateWithMinuteInterval = Calendar.current.date(byAdding: DateComponents(minute: 23), to: endTime)!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.