iphone - How to check time in between 4am to 4pm and 4pm to 4am in ios? -
nsstring *time = [self getcurrentdate]; nslog(@"current date :%@", time); nsstring *fromdate = @"10:00am"; nsstring *todate = @"10:00pm"; nsdateformatter *dateformat = [[nsdateformatter alloc]init]; [dateformat setdateformat:@"hh:mma"]; nsdate *fromtime = [dateformat datefromstring:fromdate]; nsdate *totime = [dateformat datefromstring:todate]; nsdate *nowtime = [dateformat datefromstring:time]; nscomparisonresult result1 = [nowtime compare:fromtime]; nscomparisonresult result2 = [nowtime compare:totime];
nsdateformatter *dateformat = [[nsdateformatter alloc]init]; [dateformat settimezone:[nstimezone systemtimezone]]; [dateformat setdateformat:@"hh:mma"]; nsdate *now = [nsdate date]; // current date nsstring *time = [dateformat stringfromdate:now]; nsstring *fromdate = @"10:00am"; nsstring *todate = @"11:00pm"; nsdate *fromtime = [dateformat datefromstring:fromdate]; nsdate *totime = [dateformat datefromstring:todate]; nsdate *nowtime = [dateformat datefromstring:time]; nsdatecomponents *fromcomponents = [[nscalendar currentcalendar] components:nshourcalendarunit | nsminutecalendarunit | nssecondcalendarunit fromdate:fromtime]; nsdatecomponents *tocomponents = [[nscalendar currentcalendar] components:nshourcalendarunit | nsminutecalendarunit | nssecondcalendarunit fromdate:fromtime]; nsinteger fromhour = [fromcomponents hour]; nsinteger tohour = [tocomponents hour]; if (fromhour >= 4 && tohour <= 16) { nslog(@"4am 4pm"); } else{ nslog(@"4pm 4am"); }
Comments
Post a Comment