I have a sorted array of NSDates. Can someone help with how I can find the date in an array that is closest to the current date?
I need to get the index of the nearest date so that I can scroll this date in my table view.
For example, if I have January 1, 2013, January 6, 2013, January 9, 2013, and January 10, 2013. I want the index on January 6, 2013.
Hope this makes sense. Thanks for the help.
UPDATE:
I am trying to do this:
NSTimeInterval interval = 0; NSUInteger indexOfDate; for (NSDate * date in m_arEventsSorted) { if(abs([date timeIntervalSinceDate:[NSDate date]]) < interval) { interval = abs([date timeIntervalSinceDate:[NSDate date]]); indexOfDate = [m_arEventsSorted indexOfObject:date]; } }
Lilmoke
source share