gpt4 book ai didi

ios - 如何从日期数组中获取最接近当前时间的时间? iOS, swift 3

转载 作者:行者123 更新时间:2023-11-28 06:23:04 27 4
gpt4 key购买 nike

我正在尝试从 Date 对象数组中获取最接近当前时间(小时和分钟)的时间(小时和分钟)。

我想要的不是比较并找到最近的日期,而是时间(仅小时和分钟)。是否有任何 iOS 方式/Swfit 3 可以有效地做到这一点?

编辑:最近的时间是顺时针“向前”而不是向后“移动”的时间。

编辑2:这是一个示例:

import Foundation

let userCalendar = Calendar.current

var firstDateComponents = DateComponents()
firstDateComponents.year = 2017
firstDateComponents.month = 1
firstDateComponents.day = 1
firstDateComponents.hour = 23
firstDateComponents.minute = 50
let firstDate = userCalendar.date(from: firstDateComponents)

var secondDateComponents = DateComponents()
secondDateComponents.year = 2016
secondDateComponents.month = 12
secondDateComponents.day = 24
secondDateComponents.hour = 23
secondDateComponents.minute = 55
let secondDate = userCalendar.date(from: secondDateComponents)

var thirdDateComponents = DateComponents()
thirdDateComponents.year = 2017
thirdDateComponents.month = 3
thirdDateComponents.day = 1
thirdDateComponents.hour = 0
thirdDateComponents.minute = 30
let thirdDate = userCalendar.date(from: thirdDateComponents)

var fourthDateComponents = DateComponents()
fourthDateComponents.year = 2017
fourthDateComponents.month = 3
fourthDateComponents.day = 1
fourthDateComponents.hour = 4
fourthDateComponents.minute = 20
let fourthDate = userCalendar.date(from: fourthDateComponents)

var currentDateComponents = DateComponents()
currentDateComponents.year = 2017
currentDateComponents.month = 3
currentDateComponents.day = 2
currentDateComponents.hour = 0
currentDateComponents.minute = 10

let currentDate = userCalendar.date(from: currentDateComponents)
let dates = [firstDate, secondDate, thirdDate, fourthDate]

此示例的预期结果是包含第三个日期 (12:30 AM) 的数据结构。

编辑 3:试图找到解决方案:

获取时间成分并去除不相关的日期信息:

let dateComponentsFromDates = dates.map { date -> DateComponents in
let dateComponents = DateComponents(calendar: userCalendar, timeZone: nil, era: nil, year: nil, month: nil, day: nil, hour: userCalendar.component(.hour, from: date!), minute: userCalendar.component(.minute, from: date!), second: nil, nanosecond: nil, weekday: nil, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil)
return dateComponents
}

最佳答案

您可以简单地根据它与当前时间的偏移量对日期列表进行排序,使用日历和日期组件我们可以确保我们只查看日期的小时和分钟组件:

var dates = [Date]()
// assume somehow this array is populated
let cal = Calendar.current
let nowComponents = cal.dateComponents([Calendar.Component.hour, Calendar.Component.minute], from: Date())
let nowMinsIntoDay = (nowComponents.hour ?? 0) * 60 + (nowComponents.minute ?? 0)
// Create a new list of tuples including the date and the minutes from now
let datesWithMinsFromNow = dates.map { (date) -> (Date, Int) in
let comp = cal.dateComponents([Calendar.Component.hour, Calendar.Component.minute], from: date)
let minsFromNow = (comp.hour ?? 0) * 60 + (comp.minute ?? 0) - nowMinsIntoDay
return (date, minsFromNow)
}
// Filter out any pairs that are earlier in the day than now
let datesLaterThanNow = datesWithMinsFromNow.filter { return $0.1 >= 0 }
// Sort the remaining items
let sortedDates = datesLaterThanNow.sorted { $0.1 < $1.1 }
// Take the first item
let closest = sortedDates.first?.0 // closest should now be a `Date` object

关于ios - 如何从日期数组中获取最接近当前时间的时间? iOS, swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42781038/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com