IOS: problem with NSOrderedDescending and NSOrderedAscending

I want to compare dates and I use this code

in one example, the date values ​​for my date are: date1 = 06/06/2011 date2 = 8/06/2011

if dateSelected = 06/06/2011 all this is normal, but if dateSelected = 6/06/2011 or dateSelected = 8/06/2011, the code is not written inside my "if", why ???

if (([dateSelected compare:date1] == NSOrderedDescending) &&
                ([dateSelected compare:date2]== NSOrderedAscending))
            {}
+3
source share
3 answers

NSDates represent a point in time from 01/01/2000 00:00 UTC. Thus, all these dates have time components. -compare:compares date and time.

, , 06.06. 00:00 9/6/2011 00:00. , , , 6/6/2011 00:00 . -

NSComparisonResult compareStart = [date1 compare: selectedDate]; // date1 is 6/6/2011 00:00
NSComparisonResult compareEnd = [date2 compare: selectedDate];   // date2 is 9/6/2011 00:00

if ((compareStart == NSOrderedAscending) || (compareStart == NSOrderedSame)
    && (compareEnd == NSOrderedDescending))
{
    // date is in the right range
}
+3

:

if (([date1 compare:dateSelected] == NSOrderedAscending) &&
                ([date2 compare:dateSelected]== NSOrderedDescending))
            {}
0

For those who have the same issue as @blackguardian:

Cannot initialize a parameter of type 'NSNumber *' with an lvalue of type 'NSDate *'


Something like that:

NSDate* oldDate = [NSDate date];
BOOL older = ([[NSDate date] compare:oldDate] == NSOrderedDescending);


I don’t know why, but you need to specify "[NSDate date]" in "NSDate *". It works:

NSDate* oldDate = [NSDate date];
BOOL older = ([[(NSDate*)[NSDate date] compare:oldDate] == NSOrderedDescending);
0
source

All Articles