-1

Currently I am using these group of functions to filter date objects that are used inside my app.

func isInSameWeek(date: Date) -> Bool {
    return Calendar.current.isDate(self, equalTo: date, toGranularity: .weekOfYear)
}
func isInSameMonth(date: Date) -> Bool {
    return Calendar.current.isDate(self, equalTo: date, toGranularity: .month)
}
func isInSameYear(date: Date) -> Bool {
    return Calendar.current.isDate(self, equalTo: date, toGranularity: .year)
}
func isInSameDay(date: Date) -> Bool {
    return Calendar.current.isDate(self, equalTo: date, toGranularity: .day)
}
var isInThisWeek: Bool {
    return isInSameWeek(date: Date())
}
var isInToday: Bool {
    return Calendar.current.isDateInToday(self)
}
var isInTheFuture: Bool {
    return Date() < self
}
var isInThePast: Bool {
    return self < Date()
}

They are working fine but the only issue is that I want to return date objects that are within 7 days of the current day. So if a date object occurs on the Feb 11th than it should be returned as well. Currently what I have won't return anything unless it is literally in the same calendar week.

Sateesh Yemireddi
  • 4,289
  • 1
  • 20
  • 37
Bo Jackson
  • 345
  • 1
  • 5
  • 17
  • 4
    [Check if date falls between 2 dates](https://stackoverflow.com/questions/32859569/check-if-date-falls-between-2-dates) - just create two dates +/- 7 days from the current date and compare – MadProgrammer Feb 05 '19 at 03:31
  • 1
    So basically what you wanted to say is that you copy+pasted the code from https://stackoverflow.com/a/43664156/1974224, and you need to add new functions but you don't know how? You should at least give attribution to the person you took the code from. – Cristik Feb 05 '19 at 07:54
  • If the current instant is 2019-02-01-12:00:00, would you consider 2019-02-08-14:00:00 to be “within 7 days”? There is a 7 day and 2 hour difference between those two instants. – rob mayoff Feb 05 '19 at 22:25
  • @robmayoff technically not but I just want the day as a whole – Bo Jackson Feb 06 '19 at 00:48

2 Answers2

1

This is a solution getting the end date with the nextDate(after:matching:matchingPolicy:) API of Calendar

extension Date {
    func isInSevenDays() -> Bool {
        let now = Date()
        let calendar = Calendar.current
        let weekday = calendar.dateComponents([.weekday], from: now)
        let startDate = calendar.startOfDay(for: now)
        let nextWeekday = calendar.nextDate(after: now, matching: weekday, matchingPolicy: .nextTime)!
        let endDate = calendar.date(byAdding: .day, value: 1, to: nextWeekday)!
        return self >= startDate && self < endDate
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
1

I assume you want to consider just the calendar date portion of each Date, and ignore the time of day. Thus you would consider 2019-02-01-12:00:00 to be within 7 days of 2019-02-08-14:00:00, even though there are 7 days and 2 hours between those two instants, which is more than 7 days exactly.

You can use two Calendar methods to compute the difference in calendar days between two Dates, ignoring time of day. First, get the year, month, and day components of each Date, discarding the time of day parts:

    let aymd = calendar.dateComponents([.year, .month, .day], from: a)
    let bymd = calendar.dateComponents([.year, .month, .day], from: b)

Then ask the Calendar for the difference in days between the two DateComponents:

    let diff = dateComponents([.day], from: aymd, to: bymd).day!

This diff is the signed number of days between the two input dates. It is positive if b is after a, and negative if b precedes a.

I would wrap this up in an extension on Calendar like this:

extension Calendar {

    func roundedDays(from a: Date, to b: Date) -> Int {
        let aymd = dateComponents([.year, .month, .day], from: a)
        let bymd = dateComponents([.year, .month, .day], from: b)
        let diff = dateComponents([.day], from: aymd, to: bymd).day!
        return diff
    }

}

Then you could use it like this:

let calendar = Calendar.autoupdatingCurrent

if abs(calendar.roundedDays(from: Date(), to: targetDate)) <= 7 {
    print("\(targetDate) is within 7 days of now")
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848