gpt4 book ai didi

ios - 使用当前时间从数组计算最近的时间

转载 作者:行者123 更新时间:2023-11-29 02:58:27 27 4
gpt4 key购买 nike

我在 Time 数组中有 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)];

最佳答案

我已经用自己的方法解决了,放在这里可能对别人有帮助

    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]];

}
}


}

关于ios - 使用当前时间从数组计算最近的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23591089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com