swift 3 获取最近小时的季度列表

swift 3 getting list of nearest hour quarters

好的,我在 Swift 3 中有 let date = Date(),我需要获取最近的小时列表。

假设当前日期是 2017-02-08 15:24:56 +0000,那么所需的列表将是

2017-02-08 15:30:00 +0000
2017-02-08 15:45:00 +0000
2017-02-08 16:00:00 +0000

如何获取日期列表?

编辑 1 2/8/2017 5:54 下午

这是我刚刚尝试做的事情,看起来它得到了我需要的结果。

    let currentDate = Date()
    var minute = Calendar.current.component(.minute, from: currentDate)

    if minute < 15 {
        minute = 15
    } else if minute < 30 {
        minute = 30
    } else if minute < 45 {
        minute = 45
    } else {
        minute = 0
    }

    var hourQuarterDate = Calendar.current.nextDate(after: currentDate, matching: DateComponents(minute: minute), matchingPolicy: .strict, repeatedTimePolicy: .first, direction: .forward)!

    var hourQuarters = [Date]()
    hourQuarters.append(hourQuarterDate)

    for _ in 1...10 {
        hourQuarterDate = Calendar.current.date(byAdding: .minute, value: 15, to: hourQuarterDate)!
        hourQuarters.append(hourQuarterDate)
    }

您的修改很有帮助。这是我基于它实现的一个细微变化。

我的主要步骤是将当前日期向下舍入到最新的一刻钟:

// Parameters
let minuteGranuity = 15
let numberOfDates = 4

// Find current date and date components
let now = Date()                                   
//2017-03-06 13:23:40 +0000
let calendar = Calendar.current
let hour = calendar.component(.hour, from: now)
let minute = calendar.component(.minute, from: now)

// Round down to nearest date:
let floorMinute = minute - (minute % minuteGranuity)
let floorDate = calendar.date(bySettingHour: hour, 
                              minute: floorMinute, 
                              second: 0, 
                              of: now)!

在您的编辑中,我使用 "date(byAdding: " 方法向该基准日期添加 15 分钟的间隔:

var dates: [Date] = []
for minutes in stride(from: minuteGranuity, through: minuteGranuity*numberOfDates, by: minuteGranuity) {
    let newDate = calendar.date(byAdding: .minute, value: minutes, to: floorDate)!
    dates.append(newDate)
}

for date in dates {
    print(date)
}

// 2017-03-06 13:30:00 +0000
// 2017-03-06 13:45:00 +0000
// 2017-03-06 14:00:00 +0000
// 2017-03-06 14:15:00 +0000
extension Date {
func nextFifteenMinutes() -> Date
{
    let cal = Calendar.current
    let minutes = cal.component(.minute, from: self)
    var roundedMinute = minutes
    if minutes < 15 {
        roundedMinute = 15
    } else if minutes < 30 {
        roundedMinute = 30
    } else if minutes < 45 {
        roundedMinute = 45
    } else {
        roundedMinute = 60
    }
    return cal.date(byAdding: .minute, value: roundedMinute - minutes, to: self)!
}}

用法:

 Date().nextFifteenMinutes()

我的重构解决方案测试了 swift 4 Xcode 12

extension Date {
func roundUp(totalMinutes: Int) -> Int
{
  for index in 1...3 {
    if (totalMinutes < index * 15) {return index * 15}
  }
  return 60
}
func nextFifteenMinutes() -> Date
{
   let cal = Calendar.current
   let minutes = cal.component(.minute, from: self)
   let roundedMinute = roundUp(totalMinutes: minutes)
   
   return cal.date(byAdding: .minute, value: roundedMinute - minutes, to: self)!
}}

用法:

Date().nextFifteenMinutes()