Documentation/4.Compare_Dates.md
Date comparison is available both via simple math operators (<,>,<=,>=) or throught several other functions which allows a more fined grained control of the comparison.
isToday, isTomorrow, isSameWeek, isNextYear etc.)Standard comparison between dates can be done using the classic .compare() functions or mathematical operators.
SwiftDate also introduces two additional convenience methods:
isBeforeDate(_:orEqual:granularity:) Compares whether the receiver is before/before equal date based on their components down to a given unit granularity.isAfterDate(_:orEqual:granularity:) Compares whether the receiver is after date based on their components down to a given unit granularity.While standard comparison between two dates can be done by using mathematical operators, extended comparison is made via .compare() method which offer more than 25+ different types of relevant comparisons.
func compare(_ compareType: DateComparisonType) -> Bool
takes only one argument:
compareType | DateComparisonType: the type of comparison to makeDateComparisonType is an enum which defines the type of comparison to make. This is the actual list of compare functions you can use:
For Days
isTodayisTomorrowisYesterdayisSameDay(_ : DateRepresentable)For Weeks
isThisWeekisNextWeekisLastWeekisSameWeek(_: DateRepresentable)For Months
isThisMonthisNextMonthisLastMonthisSameMonth(_: DateRepresentable)For Years
isThisYearisNextYearisLastYearisSameYear(_: DateRepresentable)For Relative Time
isInTheFutureisInThePastisEarlier(than: DateRepresentable)isLater(than: DateRepresentable)isWeekdayisWeekendFor Day Time
isMorningisAfternoonisEveningisNightFor TZ
isInDSTCONTRIBUTE! Have you a new related date you want to be part of this list? Create a new PR with the code and unit tests and we'll be happy to add it to the list!
Examples:
// return false
let _ = DateInRegion().dateAt(.endOfDay).compare(.isTomorrow)
// return true
let _ = DateInRegion() + 7.days).compare(.isNextWeek)
// return true
let _ = DateInRegion().dateAt(.startOfWeek) - 1.days).compare(.isLastWeek)
A more fined grained control for dates comparison can be obtained using the .compare(toDate:granularity:) function which offers to return a ComparisonResult value that indicates the ordering of two given dates based on their components down to a given unit granularity.
func compare(toDate refDate: DateInRegion, granularity: Calendar.Component) -> ComparisonResult
takes two arguments:
refDate | DateInRegion: date to compare against to.granularity | Calendar.Component: The smallest unit that must, along with all larger units, be less for the given datesExample:
Decides whether a Date is "close by" another one passed in parameter, where "Being close" is measured using a precision argument which is initialized with a 300 second interval (5 minute) or a specified interval.
The function is called .compareCloseTo(_:precision:) and takes two arguments:
refDate | DateInRegion reference date compare against toprecision | TimeInterval The precision of the comparison.Examples:
let date = DateInRegion("2015-01-01 04:00:00", format: dateFormat, region: regionRome)!
let refDate = DateInRegion("2015-01-01 00:00:00", format: dateFormat, region: regionRome)!
// return true because prevision is set to 5 hours and date differs for only 4 hours
let _ = dateC.compareCloseTo(refDate, precision: 5.hours.timeInterval)
Compares equality of two given dates based on their components down to a given unit granularity.
The function is called .isInside(date:granularity:) and takes two arguments:
date: date to comparegranularity: The smallest unit that must, along with all larger units, be equal for the given dates to be considered the same.Using .isInRange() function you can check if a given date is inside the range between two dates.
func isInRange(date startDate: Date, and endDate: Date, orEqual: Bool = false, granularity: Calendar.Component = .nanosecond) -> Bool
takes 4 arguments:
startDate: the lower bound limit of the rangeendDate: the upper bound limit of the rangeorEqualt: true to also validate the equalitygranularity: smallest unit that must, along with all larger units, be greater for the given datesExample:
let lowerBound = DateInRegion("2018-05-31 23:00:00", format: dateFormat, region: regionRome)!
let upperBound = DateInRegion("2018-06-01 01:00:00", format: dateFormat, region: regionRome)!
let testDate = DateInRegion("2018-06-01 00:02:00", format: dateFormat, region: regionRome)!
// return true, date is inside the hour granularity
let _ = testDate.isInRange(date: lowerBound, and: upperBound, orEqual: true, granularity: .hour)