In my application I need to compare three dates in one if-else cycle. I have a start date, end date and the date of testing. What I want to check is whether the test date is between the start date and end date.
if (testDate >= startDate && testDate <= endDate) {
If I set the startDate to 25/04/2012 and the endDate to 24/04/2019 and the testDate to 25/12/2013 the code works. But if i set the startDate to 26/04/2000 and the endDate to 24/04/2019 and the testDate to 25/12/2013 the code does not works. What’s wrong?
Dates can be compared like that. However, you must first call
timeIntervalSince1970to convert the date to an integer type value. In this case, it gives you a numeric value of typeNSTimeInterval.The other option is to use the
compare:method invoked on theNSDateobject.In that case, comparing one date to another will give a result detailed in the class reference as follows:
The receiver and anotherDate are exactly equal to each other,
NSOrderedSame
The receiver is later in time than anotherDate, NSOrderedDescending
The receiver is earlier in time than anotherDate, NSOrderedAscending.