iphone - Check if current date is between two given dates -


this question has answer here:

i want check if current date between 2 given dates. following

 nsdate *now = [nsdate new];     nslog(@"now %@",now);     nscalendar *mycalendar = [[nscalendar alloc] initwithcalendaridentifier: nsgregoriancalendar];     nsdatecomponents* components = [mycalendar components:nsyearcalendarunit|nsmonthcalendarunit|nsdaycalendarunit fromdate:[nsdate date]];     [components setyear:2013];     [components setmonth:06];     [components setday:28];     [components sethour:5];     [components setminute:00];     nsdate *startdate1 = [mycalendar datefromcomponents:components];     nsdateformatter *formatter = [[nsdateformatter alloc] init];     formatter.timezone = [nstimezone localtimezone];     formatter.dateformat = @"yyyy-mm-dd hh:mm:ss";     nslog(@"start dateeee %@",startdate1);      nsdatecomponents* components2 = [mycalendar components:nsyearcalendarunit|nsmonthcalendarunit|nsdaycalendarunit fromdate:[nsdate date]];     [components2 setyear:2013];     [components2 setmonth:06];     [components2 setday:29];     [components2 sethour:4];     [components2 setminute:59];      nsdate *enddate1 = [mycalendar datefromcomponents:components];     nslog(@"end date %@",enddate1) 

;

now have created 2 dates startdate1 , enddate1. check if current date between dates doing this.

nscomparisonresult result1 = [now compare:startdate1];     nscomparisonresult result2 = [now compare:enddate1]; if (result2 == nsorderedsame && result1 == nsorderedsame ) {         nslog(@"hell yeah! ");     } 

but never hell yeah ??

note: have set iphone datetime time between start , enddate. here logs:

2013-06-29 11:11:48.727 genk on stage[2205:907] 2013-06-29 09:11:48 +0000 2013-06-29 11:11:48.728 genk on stage[2205:907] start dateeee 2013-06-28 03:00:00 +0000 2013-06-29 11:11:48.729 genk on stage[2205:907] end date 2013-06-28 03:00:00 +0000 

your logic flawed. return value nsorderedsame means 2 values compared equal. that's not want. instead, want check if first date comes before current date , second 1 comes after it:

if (result1 == nsorderedascending && result2 == nsordereddescending) 

is should write.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -