私はこの 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);
}
}