Iphone objective-c中两个时间的比较
我有一个数组,时间如下Iphone objective-c中两个时间的比较,iphone,objective-c,ios,Iphone,Objective C,Ios,我有一个数组,时间如下 "00:00", "01:25", "02:00", "02:35", "04:35", "05:00", "06:00", "07:00", "09:00", "16:30", "17:30", "18:00", "18:30",
"00:00",
"01:25",
"02:00",
"02:35",
"04:35",
"05:00",
"06:00",
"07:00",
"09:00",
"16:30",
"17:30",
"18:00",
"18:30",
"19:30",
"21:30",
"22:00",
"22:30",
"23:00"
如何获取与当前时间接近的对象的索引?我在ObjC中没有太多的日期和时间编程经验,但我认为您可以在日期和时间编程指南部分找到相关内容。另外,请查看的文档。我在ObjC中的日期和时间编程方面没有太多经验,但我认为您可以在日期和时间编程指南中找到相关内容。如果要使用字符串数组执行此操作,请参阅。的文档,使用NSDateFormatter将此字符串转换为NSDate对象。之后,您可以将值与当前日期进行比较。您可以这样做:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
NSDate *formattedCurrentDate = [dateFormat dateFromString:[dateFormat stringFromDate:[NSDate date]]];
for(NSString *stringTime in times) {
NSDate *dateFromString = [dateFormat dateFromString:stringTime];
if([formattedCurrentDate earlierDate:dateFromString]) {
// current time is earlier that time
(...)
}
}
如果试图使用字符串数组执行此操作,请使用NSDateFormatter将此字符串转换为NSDate对象。之后,您可以将值与当前日期进行比较。您可以这样做:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
NSDate *formattedCurrentDate = [dateFormat dateFromString:[dateFormat stringFromDate:[NSDate date]]];
for(NSString *stringTime in times) {
NSDate *dateFromString = [dateFormat dateFromString:stringTime];
if([formattedCurrentDate earlierDate:dateFromString]) {
// current time is earlier that time
(...)
}
}
您可能希望使用timeIntervalSinceDate方法。在阵列中循环并跟踪最小时间间隔。用最小值存储日期
NSTimeInterval *minimumInterval;
NSDate *closestDate;
forin(NSDate *someTime in timeArray)
{
NSTimeInterval timeInt = [[NSDate date] timeIntervalSinceDate:someTime]; //someTime = any date from your array
//compare timeInterval here
if(minumumInterval > timeInt || minimumInterval == nil)
{
minimumInterval = timeInt;
closestDate = someTime;
}
}
在这个循环结束时,您将拥有从数组到当前时间最近的日期
这将为您带来好处。您可能需要使用TimeIntervalsIncestate方法。在阵列中循环并跟踪最小时间间隔。用最小值存储日期
NSTimeInterval *minimumInterval;
NSDate *closestDate;
forin(NSDate *someTime in timeArray)
{
NSTimeInterval timeInt = [[NSDate date] timeIntervalSinceDate:someTime]; //someTime = any date from your array
//compare timeInterval here
if(minumumInterval > timeInt || minimumInterval == nil)
{
minimumInterval = timeInt;
closestDate = someTime;
}
}
在这个循环结束时,您将拥有从数组到当前时间最近的日期
这将为您带来好处。您可以像这样获得时差
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinCalendarUnit) fromDate:today];
[components setHour:otherHour];
[components setMinute:otherMinute];
NSDate *otherDate = [gregorian dateFromComponents:components];
NSTimeInterval timeDifferenceBetweenDates = [today timeIntervalSinceDate:otherDate];
在你所有的小时/分钟内都这样做,并取最小的绝对时间间隔。你可以这样得到时差
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinCalendarUnit) fromDate:today];
[components setHour:otherHour];
[components setMinute:otherMinute];
NSDate *otherDate = [gregorian dateFromComponents:components];
NSTimeInterval timeDifferenceBetweenDates = [today timeIntervalSinceDate:otherDate];
P> >在你的所有小时/分钟内,以最小的绝对时间间隔。 < P>我不是C++中最好的,但是我可以给你一点提示:我将遵循: 这可以像一些字节到kb/mb/gb解析器一样完成,我的方法是将时间改为“军事”时间,这样可以将值存储为整数,如下所示:
0000,
0125,
0200,
0235,
...
这当然有一个明显的缺陷,0000低于2300,但这里是:
如果然后在阵列上进行调整并比较值:
假设时间是12:00,当然应该转换为1200,所以:
int array_length = arr.length; // we store this for later use
int cur_time = 1200;
for (int i=0; i<array_length; i++)
{
if (cur_time > arr[i] and < arr[i+1])
{
// this should be close enough. first time around.
int array_index = i;
}
}
然后我们应该(在我们的示例中):
现在我们找到最接近我们想要的值:
int highest_value = higher_value - cur_time; // 430;
int lowest_value = cur_time - lower_value; // 300;
if (highest_value > lowest_value)
{
// we have a winner, the assumed value was closest!
}
else
{
// otherwise the highest_value is the closest one, but not in our use case
}
我想我只是在这里编了自己的语言。< / P> < P>我不是C++的最佳,但我可以给你一点提示:我将遵循: 这可以像一些字节到kb/mb/gb解析器一样完成,我的方法是将时间改为“军事”时间,这样可以将值存储为整数,如下所示:
0000,
0125,
0200,
0235,
...
这当然有一个明显的缺陷,0000低于2300,但这里是:
如果然后在阵列上进行调整并比较值:
假设时间是12:00,当然应该转换为1200,所以:
int array_length = arr.length; // we store this for later use
int cur_time = 1200;
for (int i=0; i<array_length; i++)
{
if (cur_time > arr[i] and < arr[i+1])
{
// this should be close enough. first time around.
int array_index = i;
}
}
然后我们应该(在我们的示例中):
现在我们找到最接近我们想要的值:
int highest_value = higher_value - cur_time; // 430;
int lowest_value = cur_time - lower_value; // 300;
if (highest_value > lowest_value)
{
// we have a winner, the assumed value was closest!
}
else
{
// otherwise the highest_value is the closest one, but not in our use case
}
哦,顺便说一句。我想我只是在那里编了自己的语言。你可能不想这么做。不管你想做什么,我肯定有更好的办法。Def查看NSDate参考文档。您可能不想这样做。不管你想做什么,我肯定有更好的办法。Def查看NSDate参考文档。他正在寻找目标c解决方案。他正在寻找目标c解决方案。