iphone - Compare two NSDates for same date/time -
this question has answer here:
- how compare 2 dates in objective-c 14 answers
is date1 == date2 not valid way compare? if not, correct alternative?
here's code:
- (nsdate*) datewithnotime { unsigned int flags = nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit; nscalendar* calendar = [nscalendar currentcalendar]; nsdatecomponents* components = [calendar components:flags fromdate:self]; nsdate* dateonly = [calendar datefromcomponents:components]; return dateonly; } - (bool) samedayasdate:(nsdate*)datetocompare { nsdate *date1 = [self datewithnotime]; nsdate *date2 = [datetocompare datewithnotime]; return date1 == date2; // here things seem fail }
you're comparing 2 pointer values. need use nsdate comparison method like:
return ([date1 compare:date2] == nsorderedsame);
Comments
Post a Comment