I have two dates and I want to compare it.
How can I compare dates?
I have to date objects. Say modificateionDate
older updatedDate
.
So which is the best practice to compare dates?
I have two dates and I want to compare it.
How can I compare dates?
I have to date objects. Say modificateionDate
older updatedDate
.
So which is the best practice to compare dates?
Date
now conforms toComparable
protocol. So you can simply use<
,>
and==
to compare twoDate
type objects.Swift iOS 8 and up When you need more than simply bigger or smaller date comparisons. For example is it the same day or the previous day,...
Note: Never forget the timezone. Calendar timezone has a default, but if you do not like the default, you have to set the timezone yourself. To know which day it is, you need to know in which timezone you are asking.
Use it like this:
For a more complex example how to use this in a filter see this:
https://stackoverflow.com/a/45746206/4946476
Per @NiravD's answer,
Date
isComparable
. However, if you want to compare to a given granularity, you can useCalendar
'scompare(_:to:toGranularity:)
Example…
Swift has ComparisonResult with orderedAscending, orderedDescending and same as below.
Hope this may help you.