简体   繁体   English

Swift 两个日期之间的分钟间隔

[英]Swift Minute intervals between two dates

I would like to return an array of dates where each date is a 15 minute interval between a startDate and an endDate我想返回一个日期数组,其中每个日期是 startDate 和 endDate 之间的 15 分钟间隔

ie: startDate time is 2pm and endDate time is 3pm , desired result would be an array containing the following dates [2:15pm, 2:30pm, 2:45pm] in Date format.即: startDate 时间是2pm和 endDate 时间是3pm ,所需的结果将是一个包含以下日期[2:15pm, 2:30pm, 2:45pm]日期格式的数组。

I mustered up some skeleton code but have not yet been able to achieve my desired result.我收集了一些骨架代码,但还没有达到我想要的结果。

private func getTimeIntervals(startTime: Date, endTime: Date) -> [Date] {
        let timeIntervals = []
        //get both times sinces refrenced date and divide by 60 to get minutes
        let startTimeMinutes = startTime.timeIntervalSinceReferenceDate/60
        let endTimeMinutes = endTime.timeIntervalSinceReferenceDate/60
        
        while(startTimeMinutes < endTimeMinutes) {
            // Add each 15 minute Date time interval into the timeIntervalsArray
        }

        return timeIntervals
    }

Any help would be greatly appreciated, thanks in advance.任何帮助将不胜感激,在此先感谢。

Use the Calendar function date(byAdding:value:to:wrappingComponents:)使用日历 function date(byAdding:value:to:wrappingComponents:)

func dates(fromStart start: Date,
           toEnd end: Date,
           component: Calendar.Component,
           value: Int) -> [Date] {
    var result = [Date]()
    var working = start
    repeat {
        result.append(working)
        guard let new = Calendar.current.date(byAdding: component, value: value, to: working) else { return result }
        working = new
    } while working <= end
    return result
}

Here is that code in a functioning playground:这是一个运行良好的游乐场中的代码:

import UIKit

func dates(fromStart start: Date,
           toEnd end: Date,
           component: Calendar.Component,
           value: Int) -> [Date] {
    var result = [Date]()
    var working = start
    repeat {
        result.append(working)
        guard let new = Calendar.current.date(byAdding: component, value: value, to: working) else { return result }
        working = new
    } while working <= end
    return result
}

extension Date {
    var localDate: String {return DateFormatter.localizedString(from: self, dateStyle: .medium, timeStyle: .medium)}
}

let threePMComponents = DateComponents(year: 2021, month: 6, day: 7, hour: 15)
guard let threePM = Calendar.current.date(from: threePMComponents) else {
    fatalError()
}

let sixPMComponents = DateComponents(year: 2021, month: 6, day: 7, hour: 18)
guard let sixPM = Calendar.current.date(from: sixPMComponents) else {
    fatalError()
}

print("Start time = \(threePM.localDate)")
print("End time = \(sixPM.localDate)")


let datesInRange = dates(fromStart: threePM, toEnd: sixPM, component: .minute, value: 15)
for item in datesInRange {
    print(item.localDate)
}

The output is: output 是:

Start time = Jun 7, 2021 at 3:00:00 PM
End time = Jun 7, 2021 at 6:00:00 PM
Jun 7, 2021 at 3:00:00 PM
Jun 7, 2021 at 3:15:00 PM
Jun 7, 2021 at 3:30:00 PM
Jun 7, 2021 at 3:45:00 PM
Jun 7, 2021 at 4:00:00 PM
Jun 7, 2021 at 4:15:00 PM
Jun 7, 2021 at 4:30:00 PM
Jun 7, 2021 at 4:45:00 PM
Jun 7, 2021 at 5:00:00 PM
Jun 7, 2021 at 5:15:00 PM
Jun 7, 2021 at 5:30:00 PM
Jun 7, 2021 at 5:45:00 PM
Jun 7, 2021 at 6:00:00 PM

If you want the function to "snap" to the next even interval of the specified calendar component it would be more involved.如果您希望 function “捕捉”到指定日历组件的下一个偶数间隔,则涉及更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM