ios - Open Hours in Objective-C - Time between two times NSDate -


i trying perform segue in objective-c (xcode) ios devices, when time right between 2 other fixed times. "open hours" stores - when time right between open , close hour.

here code have been working on - of code may familiar, because found useful stuff on helped me - still can't work. doesn't perform segue when time passes starttime. should in specified time interval.

the time in 24-hour format.

 // set start time , end time nsstring *starttimestring = @"23:00"; nsstring *endtimestring = @"05:00";  // set date formatter 24-hour format nsdateformatter *formatter = [[nsdateformatter alloc]init]; [formatter setdateformat:@"hh:mm"];  // german timezone nslocale *locale = [[nslocale alloc]initwithlocaleidentifier:@"de_de"];   nsstring *nowtimestring = [formatter stringfromdate:[nsdate date]];  // set nsdates starttime, endtime , nowtime nsdate *starttime   = [formatter datefromstring:starttimestring]; nsdate *endtime  = [formatter datefromstring:endtimestring]; nsdate *nowtime     = [formatter datefromstring:nowtimestring];  [formatter setlocale:locale];  // compare endtime , starttime nowtime nscomparisonresult result1 = [nowtime compare:endtime]; nscomparisonresult result2 = [nowtime compare:starttime];  if ((result1 == nsordereddescending) &&     (result2 == nsorderedascending)){      nslog(@"time between");     [self performseguewithidentifier:@"openhours" sender:self];  } else {     nslog(@"time not between"); } 
  • thanks taking time @ question. have been searching , searching, trying , trying, no luck in making work yet. answers me.

you should little bit change code

// set nsdates starttime, endtime , nowtime         int starttime   = [self minutessincemidnight:[formatter datefromstring:starttimestring]];         int endtime  = [self minutessincemidnight:[formatter datefromstring:endtimestring]];         int nowtime     = [self minutessincemidnight:[formatter datefromstring:nowtimestring]];;          [formatter setlocale:locale];   if (nowtime < endtime && nowtime < starttime) {     nslog(@"time between"); } else if (nowtime > endtime && nowtime > starttime) {     nslog(@"time between"); } else {     nslog(@"time not between"); } 

and implement method calculating time:

-(int) minutessincemidnight:(nsdate *)date {     nscalendar *gregorian = [[nscalendar alloc]                              initwithcalendaridentifier:nsgregoriancalendar];     unsigned unitflags =  nshourcalendarunit | nsminutecalendarunit;     nsdatecomponents *components = [gregorian components:unitflags fromdate:date];      return 60 * [components hour] + [components minute]; } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -