如何在Swift中将分钟添加到当前时间

149

我刚开始学习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*
11个回答

342

两种方法:

  1. Use Calendar and date(byAdding:to:wrappingComponents:). E.g., in Swift 3 and later:

    let calendar = Calendar.current
    let date = calendar.date(byAdding: .minute, value: 5, to: startDate)
    
  2. Just use + operator (see +(_:_:)) to add a TimeInterval (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)
    
底线是,对于简单的情况,使用+/addingTimeInterval是最简单的方法,但如果您想添加更大的时间单位(例如天、月等),您可能会希望使用历法计算,因为它们会调整夏令时,而addingTimeInterval则没有。

对于Swift 2版本,请参见此答案的先前版本


这两种方法中哪一种更高效?是添加时间间隔(addingTimeInterval)还是日历计算(calendar.date(byAdding...)? - Nishant
4
“addingTimeInterval”无疑更高效(尽管可能只有在执行数百万次时才会显著),但更关键的是,如果你要讨论以天为单位的计量单位(或更长时间),那么日历表述更可取,因为它将为你进行所有夏令时调整。 - Rob

55

你可以使用日历的方法

func date(byAdding component: Calendar.Component, value: Int, to date: Date, wrappingComponents: Bool = default) -> Date?

可以创建一个 Date 扩展来将任何 Calendar.Component 添加到任何 Date 上。您可以创建一个 Date 扩展来将 x 分钟添加到您的 UIDatePicker 的日期:

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)!
    }
}
然后,您只需使用扩展方法向发送器(UIDatePicker)添加分钟即可:
let section1 = sender.date.adding(minutes: 5)
let section2 = sender.date.adding(minutes: 10)

游乐场测试:

Date().adding(minutes: 10)  //  "Jun 14, 2016, 5:31 PM"

1
.date(byAdding:value:to) 会在什么情况下返回 nil?我们可以放心地强制解包吗? - Declan McKenna
@Deco 如果给一个日期加上分钟会返回 nil 吗?如果结果日期没有出现错误,那么你是安全的 :) - Leo Dabus
3
文档没有明确说明如何返回 nil。这个问题解释了,你是正确的,在这里完全安全。 https://dev59.com/q1kS5IYBdhLWcg3w253q - Declan McKenna

31

Swift 4:

// 添加 5 分钟到日期

let date = startDate.addingTimeInterval(TimeInterval(5.0 * 60.0))

// 从日期中减去5分钟

let date = startDate.addingTimeInterval(TimeInterval(-5.0 * 60.0))

Swift 5.1:

// subtract 5 minutes from date
transportationFromDate.addTimeInterval(TimeInterval(-5.0 * 60.0))

10
extension Date {
    func withAddedMinutes(minutes: Double) -> Date {
         addingTimeInterval(minutes * 60)
    }

    func withAddedHours(hours: Double) -> Date {
         withAddedMinutes(minutes: hours * 60)
    }
}

应用场景

let anHourFromNow = Date().withAddedHours(hours: 1)
let aMinuteFromNow = Date().withAddedMinutes(minutes: 1)

6
你可以在 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)

输出:

before add time--> 2020-02-18 10:38:15
after add time--> 2020-02-18 10:43:15

5

保存这个小扩展:

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
 }
}

然后像这样直观地使用它:
let threeDaysLater = TimeInterval(3.days)
date.addingTimeInterval(threeDaysLater)

3
很遗憾,月份和年份是不正确的。7 * 4 * 12 = 336,这远少于365天。 - KlimczakM
1
可以通过以下方式计算月份和年份来改进它:self.days * 30self.days * 365。这并不完美,因为一年可能有366天,一个月也可能不是30天,但仍然比28/336更好。 - KlimczakM
3
基于以上列出的各种原因,你绝对不应使用此方法,因为它在许多情况下都存在不准确的问题。请使用内置的数据算术函数。 - Steven Stefanik
1
月份并不总是等于4周,那么闰年又该怎么处理呢? - Mehdi Chennoufi

4

NSDate.inittimeIntervalSinceNow 一起使用:
例如:

 let dateAfterMin = NSDate.init(timeIntervalSinceNow: (minutes * 60.0))

4
您可以使用NSDateComponents进行日期计算。例如:
import Foundation

let comps = NSDateComponents()

comps.minute = 5

let cal = NSCalendar.currentCalendar()

let r = cal.dateByAddingComponents(comps, toDate: NSDate(), options: nil)

当你在 playground 上尝试时,它就是你看到的东西。

enter image description here


3

Swift 3:

let minutes: TimeInterval = 1 * 60
let nowPlusOne = Date() + minutes

3
我认为最简单的方法是:
let minutes = Date(timeIntervalSinceNow:(minutes * 60.0))

这比创建日历和执行复杂操作要容易得多,谢谢你! - MBH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接