简体   繁体   English

如何在swift2中将分钟添加到自定义时间

[英]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.

相关问题 如何在swift2中使用Google Map在自定义markerinfowindow上添加按钮? - How to add button on custom markerinfowindow with google map in swift2? 如何快速将分钟添加到当前时间 - How to add minutes to current time in swift swift2的NSPredicate最大时间 - NSPredicate Maximum time with swift2 如何在 Swift2 中将后退按钮更改为自定义图像 - How change the back button to a custom image in Swift2 如何在Swift2和Xcode7 beta中添加用于capturesession的输入设备? - How to add input device for capturesession in Swift2 and Xcode7 beta? 经过一定时间后,如何在swift2中的xcode上关闭相机界面并打开其他视图控制器? - How do I close the camera interface after a certain time on xcode in swift2 and open a different view controller? Swift2 iOS9-如何在我的代码中获取自定义图像而不是默认注释红色图钉 - Swift2 iOS9 - how to get custom image instead of default annotation red pin in my code 如何通过NSNumber属性将自定义数据对象排序为空数组-Swift2 iOS - How to sort custom data objects into an empty array by NSNumber property -Swift2 iOS 如何在Swift 2.0中以分钟为单位获取时差 - How to get time difference in minutes in Swift 2.0 Swift 3-如何将时间显示为字符串(每5分钟) - Swift 3 - How to display time as String (each 5 minutes)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM