I have got 6 objects in array of Time
NSMutableArray *time=[[NSMutableArray alloc]initwithObjects:12:30, 14:40, 16:50, 20:30, 21:49, 23:12,nil];
Current time = 17:12;
How should I calculate the nearest time with 8:12 from time_array ?
conclusion must be 20:30
I implemented many formula's like picking up the least value available after substracting the times but there is bugs in it and its not a perfect solution. Can any one share there idea's to calculate the perfect nearest time in Objective C?
I have tried this
//// Calculating to fetch nearest time
NSDateFormatter *current_time_format = [[NSDateFormatter alloc] init];
[current_time_format setTimeZone:zone];
[current_time_format setDateFormat:@"HHmm"];
NSString *current_date_string=[current_time_format stringFromDate:[NSDate date]];
int current_time_int=[current_date_string intValue];
int nearest_time=10000;
int time_calculation=0;
if ([calculated_time_array count]>=6) {
[calculated_time_array removeAllObjects];
}
for (int i=0; i<[current_timings_array count]; i++) {
NSString *prayer_timings=[current_timings_array objectAtIndex:i];
prayer_timings=[prayer_timings stringByReplacingOccurrencesOfString:@":" withString:@""];
time_calculation=[prayer_timings intValue]-current_time_int;
NSLog(@"%d-%d=%d",[prayer_timings intValue],current_time_int,time_calculation);
NSString *length_value=[NSString stringWithFormat:@"%d",current_time_int];
NSLog(@"lenght_value= %d",[length_value length]);
[calculated_time_array addObject:[NSString stringWithFormat:@"%d",time_calculation]];
if (time_calculation<nearest_time && time_calculation>0) {
nearest_time=time_calculation;
}
NSString *time_string=[NSString stringWithFormat:@"%d",nearest_time];
int index=[calculated_time_array indexOfObject:time_string];
header_bg_image.image=[UIImage imageNamed:[NSString stringWithFormat:@"top%d_bg.png",index+1]];
NSMutableString *mu = [NSMutableString stringWithString:[NSString stringWithFormat:@"%d",nearest_time]];
if ([time_string length]==3) {
[mu insertString:@"0" atIndex:0];
[mu insertString:@":" atIndex:2];
}
NSString *time=[NSString stringWithFormat:@"%@",mu];
time=[current_timings_array objectAtIndex:index];
time=[time stringByReplacingOccurrencesOfString:@":" withString:@"."];
NSDate *date1;
NSDate *date2;
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh.mm"];
date1 = [formatter dateFromString:time];
date2 = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
}
// Here I'm calculating the time difference
NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970];
int hour = interval / 3600;
int minute = (int)interval % 3600 / 60;
// NSLog(@"%@ %dh %dm", interval<0?@"-":@"+", ABS(hour), ABS(minute));
label_time_left.text=[NSString stringWithFormat:@"Time left %dh %dm",ABS(hour), ABS(minute)];
I have solved it with my own method, here it is as it may help someone else
I assume you have NSDate objects in your array. You could make a for cycle: