Fouad
Fouad

Reputation: 409

Find if the current date is between a weekly range

I have the following range:

Weekday Hour:Minute --> Weekday Hour:Minute

Weekday is an integer from 1 (Monday) to 7 (Sunday)

For example, a given range can be the following:

I'm trying to find if the current date is in the selected range.

I tried multiple ways like creating NSDates from the ranges and comparing them but it still didn't pass all the tests.

Upvotes: 0

Views: 116

Answers (5)

Abhinav Khanduja
Abhinav Khanduja

Reputation: 116

you can try this extension:-

    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"
        }
    }
}

Upvotes: -1

LorenzOliveto
LorenzOliveto

Reputation: 7936

You could do something like this:

First create the start and end dates from hour, minute and weekday. If start date results after end date go back to 1 week. Then compare the current date to start and end dates calculated to see if it is in range.

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
}

Upvotes: 1

vadian
vadian

Reputation: 285290

A solution is to use date components. Your last example Monday 20:00 --> Monday 10:00 is

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

Then get the next occurrence of the first components from the current date backward and the second forward

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)!

and create a DateInterval and check if the current date is in the range

let isInRange = DateInterval(start: startDate, end: endDate).contains(now)

Upvotes: 1

Amyth
Amyth

Reputation: 383

It can be done using Calendar's nextDate function. Eg: here I am checking if my current time falls in between Monday 8:00 --> Friday 18:00

//Monday 8:00 --> Friday 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

Upvotes: 1

Sean Lintern
Sean Lintern

Reputation: 3141

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
     }
}

Upvotes: 0

Related Questions