[objective-c]最近的时间从数组与当前时间计算

标签: ios Objective-C
发布时间: 2016/10/4 6:53:17
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 6 对象数组的时间中

NSMutableArray *time=[[NSMutableArray alloc]initwithObjects:12:30, 14:40, 16:50, 20:30, 21:49, 23:12,nil];

Current time = 17:12;

我应如何计算 8:12 从 time_array 的最近时间?

conclusion must be 20:30 

我实现了很多公式捡可用的最小值后乘除但有的时候是 bug 在它,它不, 像一个完美的解决方案。 可以任何一股有想法的来计算最近的时间在目标 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)];

解决方法 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]];

        }
        }


    }
官方微信
官方QQ群
31647020