ios - If statement with dates -
what trying make if statement dates using greater less signs. reason greater sign works. here code:
nsdate *currdate = [nsdate date]; nsdateformatter *dateformatter = [[nsdateformatter alloc]init]; [dateformatter setdateformat:@"hhmm"]; nsstring *datestring = [dateformatter stringfromdate:currdate]; nslog(@"%@",datestring); if (datestring < @"0810" && datestring > @"0800") { nslog(@"homeroom"); } else { nslog(@"no"); }
the output code if time 8:03:
2013-04-08 08:03:47.956 schedule2.0[13200:c07] 0803 2013-04-08 08:03:47.957 schedule2.0[13200:c07] no
if make greater sign this:
if (datestring > @"0800") { nslog(@"homeroom"); } else { nslog(@"no"); }
the output this:
2013-04-08 08:03:29.748 schedule2.0[14994:c07] 0803 2013-04-08 08:03:29.749 schedule2.0[14994:c07] homeroom
create nsdate object time 8:10 , 1 8:00. can compare given date both these dates
if(([date0800 compare:date] == nsorderingascending) && [date0810 compare:date] == nsorderingdescending) ) { // date between other }
to create boundaries dates can this
nsdate *date = [nsdate date]; // nsdatecomponents *components = [[nscalendar currentcalendar] components:( nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit ) fromdate:date]; components.hour = 8; components.minute = 0; nsdate *date0800 = [[nscalendar currentcalendar] datefromcomponents: components]; components.minute = 10; nsdate *date0810 = [[nscalendar currentcalendar] datefromcomponents: components];
if insist of using operators <
, >
, can use timeinterval of date objects.
if(([date0800 timeintervalsince1970] < [date timeintervalsince1970]) && ([date0810 timeintervalsince1970] > [date timeintervalsince1970])) { // date lays between other 2 }
but beware of checking ==
on it, faulty due rounding errors.
Comments
Post a Comment