last day (15 days later) » 

7:08 PM
0
A: Check whether time falls between two time iOS

Uttam SinhaTry this - NSString *startTimeString = @"08:00 AM"; NSString *endTimeString = @"06:00 PM"; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"hh:mm a"]; NSString *nowTimeString = [formatter stringFromDate:[NSDate date]]; int startTime = [self minutesSince...

 
thanks for the answer, but it gives me error here int nowTime = (currHr*60)+currtMin; implicit conversion loses integer precision long to int
 
i have already mentioned convert NSInteger to (int). int nowTime = (int)(currHr*60)+(int)currtMin
 
Dude integer conversion woks but, your 2nd if condition gets false when nowTime is 1:15pm between 8:35am and 4.:43pm :(
 
First of all you have to use 24Hr format and am/pm logic wont work in checking time whether it falls between start time & end time or not. Why this, because if you check for time 4:00 pm between 4:00 am & 5:00 pm. Minutes conversion of 4:00 am & 4:00 pm will give same value and logic fails here. So think over it and use 24Hr format. If you really want to use am/pm logic then use date also with time there is different logic for checking datetime :)
 
Time is coming from server i can't change any thing in that :( only fix thing is startTime always in am and endTime in pm now can you please help me in the logic ?
 
7:08 PM
check updated answer. hope this what you are looking for.
 
Bro your code crashes -[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x7fbfebfb6ae0 here NSDateComponents *components = [gregorian components:unitFlags fromDate:date]; value of unitFlag is 96 and date is 8:16
 
check the attached screenshot. code works fine for me no crashes. you can check with other identifier "NSCalendarIdentifierGregorian" in place of "NSCalendarIdentifierIndian". I have checked with both the identifier.
check the updated code. without any identifier also code works fine with expected result :)
 
hello
dude i am facing the same error again
here is my code
:(
NSArray *arr=[[obj getFromToDTime] componentsSeparatedByString:@"to"];


NSDate *timeA, *timeB;

timeA=[arr objectAtIndex:0]; //8:45
timeB=[arr objectAtIndex:1]; //4:43
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"hh:mm"];

NSString *nowTimeString = [formatter stringFromDate:[NSDate date]];

int startTime = [self minutesSinceMidnight:timeA];
int endTime = [self minutesSinceMidnight:timeB];
int nowTime = [self minutesSinceMidnight:[formatter dateFromString:nowTimeString]];;


if (startTime <= nowTime && nowTime <= endTime)
{
NSLog(@"Time is between");
}
else {
NSLog(@"Time is not between");
}
nowTimeString is 3:30
-(int) minutesSinceMidnight:(NSDate *)date
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
return 60 * (int)[components hour] + (int)[components minute];
}
 

  last day (15 days later) »