如何快速增加分钟到当前时间

我是新来的斯威夫特和正在尝试一个调度程序。我已经选择了开始时间,我需要添加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*
146689 次浏览

可以使用 Calendar 的方法

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

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

Xcode 8 and Xcode 9• Swift 3.0 and 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. 使用 Calendardate(byAdding:to:wrappingComponents:),例如,在 Swift 3和更高版本中:

    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
    

    (注意,这里的顺序是特定的: +左边的日期和右边的秒。)

    如果你愿意,你也可以使用 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.


For Swift 2 renditions, see the previous revision of this answer.

你可以使用 NSDateComponents进行日期算术。例如:

import Foundation


let comps = NSDateComponents()


comps.minute = 5


let cal = NSCalendar.currentCalendar()


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

这是你在操场上看到的

enter image description here

NSDate.inittimeIntervalSinceNow:
例如:

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

保存这个小扩展:

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:

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

斯威夫特4:

//添加5分钟到日期

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

//从日期减去5分钟

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

迅捷5.1:

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

如果你想要 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

我觉得最简单的就是

let minutes = Date(timeIntervalSinceNow:(minutes * 60.0))

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