Note that there are some explanatory texts on larger screens.

plurals
  1. PONSString of comma separated days to NSDate calculate closest day to current day
    primarykey
    data
    text
    <p>I have a string like this 13, 27, 29 representing days of the month I want to separate them like below into date objects</p> <pre><code>mayString = [mayString componentsSeparatedByString:@","]; </code></pre> <p>I then want to be able to work out which of these days i.e 13 or 27 or 29 is closest to todays date which obviously taking the above dates would be 27 as the next closest date to current date.</p> <p>I can grab current day using the below but really stuck on how to get the logic to do this?</p> <pre><code>//Grab current day from sys date NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init]; [dayFormatter setDateFormat:@"dd"]; NSString *dayString = [dayFormatter stringFromDate:[NSDate date]]; </code></pre> <p>I have a partial completed solution but it doesnt seem to give me the correct result of what index in the array is closest to current day (sepDates is an array)</p> <pre><code>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 &lt; [sepDates count]; ++d) { double currentmin = [dayFromString timeIntervalSinceDate:[sepDates objectAtIndex:d]]; if (currentmin &lt; min) { min = currentmin; minIndex = d; NSLog(@"minIndex = %d", minIndex); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload