我有时间数组中的6个对象
NSMutableArray *time=[[NSMutableArray alloc]initwithObjects:12:30, 14:40, 16:50, 20:30, 21:49, 23:12,nil];
Current time = 17:12;
如何计算从time_array到8:12的最近时间?
conclusion must be 20:30
我实施了许多公式,比如在减去时间之后获得最少的可用值,但是它存在缺陷并且它不是一个完美的解决方案。 任何人都可以分享那些在Objective C中计算完美最近时间的想法吗?
我试过这个
//// 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)];
答案 0 :(得分:1)
我用自己的方法解决了这个问题,这可能对其他人有帮助
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:zone];
[formatter setDateFormat:@"HHmm"];
current_time=[formatter stringFromDate:date];
int current_time_int=[current_time intValue];
nearest_time=2400;
for (int i=0; i<[current_timings_array count]; i++) {
NSString *prayer_timings=[current_timings_array objectAtIndex:i];
prayer_timings=[prayer_timings stringByReplacingOccurrencesOfString:@":" withString:@""];
int calculated_time=[prayer_timings intValue]-current_time_int;
NSLog(@"Nearest Timings %d-%d=%d",[prayer_timings intValue],current_time_int ,calculated_time);
NSString *last_object_time=[current_timings_array.lastObject stringByReplacingOccurrencesOfString:@":" withString:@""];
if (current_time_int>[last_object_time intValue]) {
NSLog(@"It is greated value %d",[last_object_time intValue]);
if (calculated_time<nearest_time ) {
nearest_time=calculated_time;
header_bg_image.image=[UIImage imageNamed:[NSString stringWithFormat:@"top%d_bg.png", i+1]];
}
}
else{
if (calculated_time<nearest_time && calculated_time>0) {
nearest_time=calculated_time;
header_bg_image.image=[UIImage imageNamed:[NSString stringWithFormat:@"top%d_bg.png", i+1]];
}
}
}
答案 1 :(得分:0)
我假设你的数组中有NSDate对象。 你可以做一个for循环:
NSDate *minDate = time.lastObject;
NSTimeInterval minTimeInterval = [minDate timeIntervalSince1970];
NSTimeInterval current = [[NSDate date] timeIntervalSince1970];
for (NSDate *date in time) {
NSTimeInterval interval = [date timeIntervalSince1970];
if (labs(minTimeInterval - current) > labs(interval - current)) {
minDate = date;
minTimeInterval = interval;
}
}
// The minDate is the object you are looking for