检查给定日期与日期数组相比是否最接近当前日期和时间
我见过这样的问题,但从大约 6 年前开始,我认为并没有直接回答这个问题。
我有一个遵循以下模型的“事件”数组:
struct Event: Identifiable{
var id = UUID().uuidString
var eventTitle: String
var eventDescription: String
var eventDate: Date
}
和示例数据:
@Published var storedEvents: [Event] = [
Event(eventTitle: "Go shopping", eventDescription: "Eggs, flour, butter", eventDate: .init(timeIntervalSince1970: 1646078400)),
Event(eventTitle: "Lecture with bob", eventDescription: "Room 453", eventDate: .init(timeIntervalSince1970: 1646078708))
]
对于任何给定的事件,我希望将事件的日期传递给接受 Date
并返回 true 的函数或 false 给定的事件日期是否是当前日期时间与所有其他事件的日期相比最接近/当前的事件(即,如果我有两个事件,一个为 8:15,一个为 8:30,并且当前时间为 8: 21、然后是第一个事件应该是当前事件,因此从我的函数返回 true
我有一个只考虑小时的函数,因此如果两个事件具有相同的小时,那么它们都会被选择并且都返回 true:
func isCurrentHour(date: Date)-> Bool{
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let currentHour = calendar.component(.hour, from: Date())
return hour == currentHour
}
我已经尝试过。对日期列表从最早到最晚进行排序,然后将每个日期与当前时间进行比较,但我不确定如何在比较的正确事件上返回 true
或 false
因为我无法确定哪个日期属于某个事件。
I have seen questions like these, but from like 6 years ago and do not directly answer the question in my opinion.
I have an array of 'events' which follow the Model of:
struct Event: Identifiable{
var id = UUID().uuidString
var eventTitle: String
var eventDescription: String
var eventDate: Date
}
And example data of:
@Published var storedEvents: [Event] = [
Event(eventTitle: "Go shopping", eventDescription: "Eggs, flour, butter", eventDate: .init(timeIntervalSince1970: 1646078400)),
Event(eventTitle: "Lecture with bob", eventDescription: "Room 453", eventDate: .init(timeIntervalSince1970: 1646078708))
]
For any given event, I'd like the event's date to be passed to a function that takes Date
and returns true or false whether the given events date is the closest/current event at the current date time compared to all other event's dates (i.e. if I had two events, one for 8:15 and one for 8:30 and the current time is 8:21, then the first event should be the current one, and therefore returned true from my function.
I have a function working that only takes into account the hour, so if two events have the same hour then they will both be selected and both return true:
func isCurrentHour(date: Date)-> Bool{
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let currentHour = calendar.component(.hour, from: Date())
return hour == currentHour
}
I have tried sorting the list of dates from the earliest to latest and then comparing each to the current time but I'm not sure how to return true
or false
on the right event being compared as I have no way of identifying which date belongs to an event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你可能会遇到困难。看来您真正想要的是所有事件中最早的事件。如果是这样,可以这样完成:
首先,使您的
Event
结构符合Comparable
by date:然后您可以返回数组中的最小元素。以下将及时返回最早的事件,但请记住,如果您需要的话,不会对其进行检查以确保它是最早的未来事件。
这会返回一个可选的
Event
,因为它可能会失败,例如在数组为空的情况下。编辑:
I think you may be going about this the hard way. It seems what you really want is the earliest event from all events. If so, that can be accomplished like this:
First, make your
Event
struct conform toComparable
by date:Then you can return the minimum element in the array. The following will return the earliest event in time, but remember that there is no check in it to make sure that it is the earliest future event, if that is what you need.
This returns an optional
Event
as it could fail, such as in the case where the array is empty.Edit:
我建议作为一种解决方案,只需从迄今为止的日期中获取间隔,然后比较两个间隔。
文档此处
I suggest as a solution to just take the intervals from the dates so far and then compare the two intervals.
Documentation here