Check if date falls between 2 dates

Swift ≧ 3

Swift 3 makes this a lot easier.

let fallsBetween = (startDate ... endDate).contains(Date())

Now that NSDate is bridged to the value type Date and Date conforms to Comparable we can just form a ClosedRange<Date> and use the contains method to see if the current date is included.

Caveat: endDate must be greater or equal startDate. Otherwise the range could not be formed and the code would crash with a fatalError.

This is safe:

extension Date {
    func isBetween(_ date1: Date, and date2: Date) -> Bool {
        return (min(date1, date2) ... max(date1, date2)).contains(self)

Leave a Comment