Swift iOS 8 and . If you need more than just bigger or smaller date mappings. For example, this is the same day or the previous day, ...
Note: Never forget the time zone. The time zone of the calendar has a default value, but if you do not like the default value, you must set the time zone yourself. To find out which day you need to know what time zone you are asking.
extension Date { func compareTo(date: Date, toGranularity: Calendar.Component ) -> ComparisonResult { var cal = Calendar.current cal.timeZone = TimeZone(identifier: "Europe/Paris")! return cal.compare(self, to: date, toGranularity: toGranularity) } }
Use it as follows:
if thisDate.compareTo(date: Date(), toGranularity: .day) == .orderedDescending {
For a more complex example of using this in a filter, see this:
fooobar.com/questions/44168 / ...
t1ser source share