我有一个像这样的字符串 13, 27, 29 代表一个月中的几天我想像下面这样将它们分隔成日期对象
mayString = [mayString componentsSeparatedByString:@","];
然后,我希望能够计算出这些日子中的哪一天,即 13 或 27 或 29 最接近今天的日期,显然上述日期将 27 作为与当前日期的下一个最接近的日期。
我可以使用下面的方法来获取当前日期,但真的坚持如何获得执行此操作的逻辑?
//Grab current day from sys date
NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init];
[dayFormatter setDateFormat:@"dd"];
NSString *dayString = [dayFormatter stringFromDate:[NSDate date]];
我有一个部分完成的解决方案,但它似乎没有给我数组中最接近当前日期的索引的正确结果(sepDates 是一个数组)
sepDates = [mayString componentsSeparatedByString:@","];
//Day string is todays date i.e 16 (16th)
NSDate *dayFromString = [dayFormatter dateFromString:dayString];
NSLog(@"Day from string %@", dayFromString);
double min = [dayFromString timeIntervalSinceDate:[sepDates objectAtIndex:0]];
NSLog(@"Min %f", min);
//I then want to calculate which of the dates in the sepDates array at index is closest to todays current day 16
int minIndex = 0;
for (int d = 1; d < [sepDates count]; ++d)
{
double currentmin = [dayFromString timeIntervalSinceDate:[sepDates objectAtIndex:d]];
if (currentmin < min) {
min = currentmin;
minIndex = d;
NSLog(@"minIndex = %d", minIndex);
}
}