查找当前日期是否在每周范围内

Find if the current date is between a weekly range

我有以下范围:

工作日 Hour:Minute --> 工作日 Hour:Minute

工作日是从 1(星期一)到 7(星期日)的整数

例如,给定范围可以是以下内容:

我正在尝试查找当前日期是否在所选范围内。

我尝试了多种方法,例如从范围创建 NSDates 并进行比较,但它仍然没有通过所有测试。

一种解决方案是使用日期组件。你的最后一个例子 Monday 20:00 --> Monday 10:00

let startComponents = DateComponents(hour:20, minute:0, weekday:1)
let endComponents = DateComponents(hour:10, minute:0, weekday:1)

然后从当前日期向后和第二个向前获取第一个组件的下一次出现

let now = Date()
let startDate = Calendar.current.nextDate(after: now, matching: startComponents, matchingPolicy: .nextTime, direction: .backward)!
let endDate = Calendar.current.nextDate(after: startDate , matching: endComponents, matchingPolicy: .nextTime)!

并创建一个 DateInterval 并检查当前日期是否在

范围内
let isInRange = DateInterval(start: startDate, end: endDate).contains(now)
extension Date {
    func isSameWeek(as date: Date) -> Bool {
        let dateComponents = Calendar.current.dateComponents([.day, .weekOfYear, .year], from: date)
        let currentDateComponents = Calendar.current.dateComponents([.day, .weekOfYear, .year], from: self)
        return dateComponents.year == currentDateComponents.year && dateComponents.weekOfYear == currentDateComponents.weekOfYear
     }
}

你可以这样做:

首先根据小时、分钟和工作日创建开始和结束日期。如果开始日期结果在结束日期之后返回 1 周。然后将当前日期与计算的开始日期和结束日期进行比较,看它是否在范围内。

let currentDate = Date()

var startDate = dateBySetting(hour: 8, minute: 0, weekday: 1, of: currentDate)
let endDate = dateBySetting(hour: 9, minute: 0, weekday: 5, of: currentDate)

// If start date results after end date remove a week from the start
if startDate > endDate {
    startDate = Calendar.current.date(byAdding: .weekOfYear, value: -1, to: startDate) ?? startDate
}

let dateIsInRange = startDate <= currentDate && currentDate <= endDate

func dateBySetting(hour: Int, minute: Int, weekday: Int, of date: Date) -> Date {
    let calendar = Calendar.current
    var date = calendar.date(bySettingHour: hour, minute: minute, second: 0, of: date) ?? date
    date = calendar.date(bySetting: .weekday, value: weekday, of: date) ?? date
    return date
}

可以使用日历的 nextDate 函数来完成。 例如:我在这里检查我的当前时间是否在星期一 8:00 --> 星期五 18:00

之间

//星期一 8:00 --> 星期五 18:00

let currentDate = Date()
let calendar = Calendar.current

let yearComponent = (calendar.dateComponents(in: .current, from: Date()).year)!

let monthComponent = (calendar.dateComponents(in: .current, from: Date()).month)!

let previousTime  = calendar.nextDate(after: currentDate,
                                     matching: DateComponents(calendar: calendar, timeZone: .current, year: yearComponent, month: monthComponent, hour: 8, minute: 0, weekday: 2),
                                     matchingPolicy: .strict,
                                     repeatedTimePolicy: .first,
                                     direction: .backward)

print(previousTime)

let nextTime  = calendar.nextDate(after: previousTime!,
                                  matching: DateComponents(calendar: calendar, timeZone: .current, year: yearComponent, month: monthComponent, hour: 18, minute: 0, weekday: 6),
                                  matchingPolicy: .strict,
                                  repeatedTimePolicy: .first,
                                  direction: .forward)

print(nextTime)


print(currentDate > previousTime! && currentDate < nextTime!) //true

你可以试试这个扩展:-

    extension Date{
      func timeAgoDisplay() -> String {
        let secondsAgo = Int(Date().timeIntervalSince(self))

        let minute = 60
        let hour = minute * 60
        let day = hour * 24
        let week = day * 7
        let month = week * 4

        if secondsAgo < 60 {
            return "\(secondsAgo) seconds ago"
        }else if secondsAgo < hour {
            return "\(secondsAgo/minute) minutes ago"
        }else if secondsAgo < day {
            return "\(secondsAgo/hour) hours ago"
        }else if secondsAgo < week {
            return "\(secondsAgo/day) days ago"
        }else if secondsAgo < month {
            return "\(secondsAgo/week) weeks ago"
        }else {
            return "\(secondsAgo/month) month(s) ago"
        }
    }
}