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

时间:2015-04-06 04:07:13

标签: ios swift

我是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 个答案:

答案 0 :(得分:219)

两种方法:

  1. 使用日历:

    在Swift 3及更高版本中:

    let calendar = Calendar.current
    let date = calendar.date(byAdding: .minute, value: 5, to: startDate)
    

    或者,在Swift 2中:

    let calendar = NSCalendar.currentCalendar()
    let date = calendar.dateByAddingUnit(.Minute, value: 5, toDate: startDate, options: [])
    
  2. 只需使用dateByAddingTimeInterval添加一定的秒数。

    例如,

    Swift 3或更高版本:

    let date = startDate.addingTimeInterval(5.0 * 60.0)
    

    斯威夫特2:

    let date = startDate.dateByAddingTimeInterval(5.0 * 60.0)
    
  3. 底线,addingTimeInterval最简单,但如果您要添加天,月等,则需要使用日历计算,因为这些会针对夏令时进行调整,而dateByAddingTimeInterval则不会吨。

答案 1 :(得分:33)

您可以使用日历的方法

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

Calendar.Component添加到任何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"

答案 2 :(得分:14)

Swift 4

//添加5分钟到目前为止

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

//从日期减去5分钟

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

答案 3 :(得分: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)

答案 4 :(得分:5)

您可以快速使用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 :(得分:3)

您可以使用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

答案 6 :(得分:3)

<{> NSDate.init timeIntervalSinceNow
例如:

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

答案 7 :(得分:2)

斯威夫特3:

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

答案 8 :(得分: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

答案 9 :(得分:0)

我认为最简单的是

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

答案 10 :(得分:0)

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)