检查给定日期与日期数组相比是否最接近当前日期和时间

发布于 2025-01-10 17:06:06 字数 1211 浏览 4 评论 0原文

我见过这样的问题,但从大约 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
}

我已经尝试过。对日期列表从最早到最晚进行排序,然后将每个日期与当前时间进行比较,但我不确定如何在比较的正确事件上返回 truefalse因为我无法确定哪个日期属于某个事件。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2025-01-17 17:06:06

我想你可能会遇到困难。看来您真正想要的是所有事件中最早的事件。如果是这样,可以这样完成:

首先,使您的 Event 结构符合 Comparable by date:

struct Event: Identifiable, Comparable {
    
    var id = UUID().uuidString
    var eventTitle: String
    var eventDescription: String
    var eventDate: Date
    
    static func < (lhs: Event, rhs: Event) -> Bool {
        lhs.eventDate < rhs.eventDate
    }
}

然后您可以返回数组中的最小元素。以下将及时返回最早的事件,但请记住,如果您需要的话,不会对其进行检查以确保它是最早的未来事件。

private func earliestEvent(events: [Event]) -> Event? {
    return events.min()
}

这会返回一个可选的Event,因为它可能会失败,例如在数组为空的情况下。

编辑:

private func earliestEvent(event: Event, events: [Event]) -> Bool {
    guard let earliestEvent = events.min() else { return false }
    return event == earliestEvent
}

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 to Comparable by date:

struct Event: Identifiable, Comparable {
    
    var id = UUID().uuidString
    var eventTitle: String
    var eventDescription: String
    var eventDate: Date
    
    static func < (lhs: Event, rhs: Event) -> Bool {
        lhs.eventDate < rhs.eventDate
    }
}

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.

private func earliestEvent(events: [Event]) -> Event? {
    return events.min()
}

This returns an optional Event as it could fail, such as in the case where the array is empty.

Edit:

private func earliestEvent(event: Event, events: [Event]) -> Bool {
    guard let earliestEvent = events.min() else { return false }
    return event == earliestEvent
}
愚人国度 2025-01-17 17:06:06

我建议作为一种解决方案,只需从迄今为止的日期中获取间隔,然后比较两个间隔。

func timeIntervalSince(_ date: Date) -> TimeInterval

文档此处

I suggest as a solution to just take the intervals from the dates so far and then compare the two intervals.

func timeIntervalSince(_ date: Date) -> TimeInterval

Documentation here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文