ios 如何快速将分钟添加到当前时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29465205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add minutes to current time in swift
提问by try maadee
I am new to Swift and am trying a scheduler. I have the start time selected and I need to add 5 minutes (or multiples of it) to the start time and display it in an UILabel?
我是 Swift 的新手,正在尝试调度程序。我选择了开始时间,我需要将 5 分钟(或它的倍数)添加到开始时间并将其显示在 UILabel 中?
@IBAction func timePickerClicked(sender: UIDatePicker) {
var dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
var dateStr = dateFormatter.stringFromDate(startTime.date)
let sttime = dateStr
startTimeDisplay.text = dateStr
}
// How to advance time by 5 minutes for each section based on the start time selected and display time
// section 1 = start time + 5
// section 2 = start time + 10*
回答by Rob
Two approaches:
两种做法:
Use
Calendar
anddate(byAdding:to:wrappingComponents:)
. E.g., in Swift 3 and later:let calendar = Calendar.current let date = calendar.date(byAdding: .minute, value: 5, to: startDate)
Just use
+
operator (see+(_:_:)
) to add aTimeInterval
(i.e. a certain number of seconds). E.g. to add five minutes, you can:let date = startDate + 5 * 60
(Note, the order is specific here: The date on the left side of the
+
and the seconds on the right side.)You can also use
addingTimeInterval
, if you'd prefer:let date = startDate.addingTimeInterval(5 * 60)
使用
Calendar
和date(byAdding:to:wrappingComponents:)
。例如,在 Swift 3 及更高版本中:let calendar = Calendar.current let date = calendar.date(byAdding: .minute, value: 5, to: startDate)
只需使用
+
运算符(请参阅+(_:_:)
)添加一个TimeInterval
(即特定的秒数)。例如添加五分钟,您可以:let date = startDate + 5 * 60
(注意,这里的顺序是具体的:日期在左边
+
,秒在右边。)addingTimeInterval
如果您愿意,也可以使用:let date = startDate.addingTimeInterval(5 * 60)
Bottom line, +
/addingTimeInterval
is easiest for simple scenarios, but if you ever want to add larger units (e.g., days, months, etc.), you would likely want to use the calendrical calculations because those adjust for daylight savings, whereas addingTimeInterval
doesn't.
底线,+
/addingTimeInterval
对于简单的场景是最简单的,但是如果您想添加更大的单位(例如,天、月等),您可能需要使用日历计算,因为这些计算会针对夏令时进行调整,而addingTimeInterval
不会.
For Swift 2 renditions, see the previous revision of this answer.
回答by Leo Dabus
You can use Calendar's method
您可以使用日历的方法
func date(byAdding component: Calendar.Component, value: Int, to date: Date, wrappingComponents: Bool = default) -> Date?
to add any Calendar.Component
to any Date
. You can create a Date extension to add x minutes to your UIDatePicker
's date:
将 any 添加Calendar.Component
到 any Date
。您可以创建一个 Date 扩展来为您UIDatePicker
的日期添加 x 分钟:
Xcode 8 and Xcode 9 ? Swift 3.0 and Swift 4.0
Xcode 8 和 Xcode 9 ?Swift 3.0 和 Swift 4.0
extension Date {
func adding(minutes: Int) -> Date {
return Calendar.current.date(byAdding: .minute, value: minutes, to: self)!
}
}
Then you can just use the extension method to add minutes to the sender (UIDatePicker):
然后你可以使用扩展方法向发件人添加分钟(UIDatePicker):
let section1 = sender.date.adding(minutes: 5)
let section2 = sender.date.adding(minutes: 10)
Playground testing:
游乐场测试:
Date().adding(minutes: 10) // "Jun 14, 2016, 5:31 PM"
回答by Gilad Brunfman
Swift 4:
斯威夫特 4:
// add 5 minutes to date
// 添加 5 分钟到日期
let date = startDate.addingTimeInterval(TimeInterval(5.0 * 60.0))
// subtract 5 minutes from date
// 从日期中减去 5 分钟
let date = startDate.addingTimeInterval(TimeInterval(-5.0 * 60.0))
Swift 5.1:
斯威夫特 5.1:
// subtract 5 minutes from date
transportationFromDate.addTimeInterval(TimeInterval(-5.0 * 60.0))
回答by Anthony Kong
You can do date arithmetic by using NSDateComponents
. For example:
您可以使用NSDateComponents
. 例如:
import Foundation
let comps = NSDateComponents()
comps.minute = 5
let cal = NSCalendar.currentCalendar()
let r = cal.dateByAddingComponents(comps, toDate: NSDate(), options: nil)
It is what you see when you try it in playground
这是你在操场上尝试时看到的
回答by Kumar KL
NSDate.init
with timeIntervalSinceNow
:
Ex:
NSDate.init
与timeIntervalSinceNow
:
例如:
let dateAfterMin = NSDate.init(timeIntervalSinceNow: (minutes * 60.0))
回答by Fitsyu
Save this little extension:
保存这个小扩展:
extension Int {
var seconds: Int {
return self
}
var minutes: Int {
return self.seconds * 60
}
var hours: Int {
return self.minutes * 60
}
var days: Int {
return self.hours * 24
}
var weeks: Int {
return self.days * 7
}
var months: Int {
return self.weeks * 4
}
var years: Int {
return self.months * 12
}
}
Then use it intuitively like:
然后直观地使用它,例如:
let threeDaysLater = TimeInterval(3.days)
date.addingTimeInterval(threeDaysLater)
回答by Al___
Swift 3:
斯威夫特 3:
let minutes: TimeInterval = 1 * 60
let nowPlusOne = Date() + minutes
回答by Enamul Haque
You can use in swift 4 or 5
您可以在 swift 4 或 5 中使用
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd H:mm:ss"
let current_date_time = dateFormatter.string(from: date)
print("before add time-->",current_date_time)
//adding 5 miniuts
let addminutes = date.addingTimeInterval(5*60)
dateFormatter.dateFormat = "yyyy-MM-dd H:mm:ss"
let after_add_time = dateFormatter.string(from: addminutes)
print("after add time-->",after_add_time)
output:
输出:
before add time--> 2020-02-18 10:38:15
after add time--> 2020-02-18 10:43:15
回答by Pradeep Kumar Kushwaha
In case you want unix timestamp
如果你想要 unix 时间戳
let now : Date = Date()
let currentCalendar : NSCalendar = Calendar.current as NSCalendar
let nowPlusAddTime : Date = currentCalendar.date(byAdding: .second, value: accessTime, to: now, options: .matchNextTime)!
let unixTime = nowPlusAddTime.timeIntervalSince1970
回答by Sachin Nautiyal
I think the simplest will be
我认为最简单的将是
let minutes = Date(timeIntervalSinceNow:(minutes * 60.0))