With Swift 5, you can use one of the two solutions below in order to check if a date occurs between two other dates.
#1. Using DateInterval's contains(_:) method
DateInterval has a method called contains(_:). contains(_:) has the following declaration:
func contains(_ date: Date) -> Bool
Indicates whether this interval contains the given date.
The following Playground code shows how to use contains(_:) in order to check if a date occurs between two other dates:
import Foundation
let calendar = Calendar.current
let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))!
let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))!
let myDate = calendar.date(from: DateComponents(year: 2012, month: 8, day: 15))!
let dateInterval = DateInterval(start: startDate, end: endDate)
let result = dateInterval.contains(myDate)
print(result) // prints: true
#2. Using ClosedRange's contains(_:) method
ClosedRange has a method called contains(_:). contains(_:) has the following declaration:
func contains(_ element: Bound) -> Bool
Returns a Boolean value indicating whether the given element is contained within the range.
The following Playground code shows how to use contains(_:) in order to check if a date occurs between two other dates:
import Foundation
let calendar = Calendar.current
let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))!
let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))!
let myDate = calendar.date(from: DateComponents(year: 2012, month: 8, day: 15))!
let range = startDate ... endDate
let result = range.contains(myDate)
//let result = range ~= myDate // also works
print(result) // prints: true