NSDate finds the closest date for today

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]; } } 
+7
source share
5 answers

NSDate provides a timeIntervalSinceNow ( reference ) method that returns NSTimeInterval (a typedef 'd double ). Just use the fabs() function to find the smallest difference between each date now.

+7
source

I would recommend the timeIntervalSinceDate: function. Something like this (code not verified!):

 NSDate *currentDate = [NSDate date]; double min = [currentDate timeIntervalSinceDate:[array objectAtIndex:0]]; int minIndex = 0; for (int i = 1; i < [array count]; ++i) { double currentmin = [currentDate timeIntervalSinceDate:[array objectAtIndex:i]]; if (currentmin < min) { min = currentmin; minIndex = i; } } 

You can get the closest index from minIndex.

+2
source

This should work:

 NSDate *closestDate; for( NSDate *tempDate in dates ){ NSInteger tempDateInterval = [tempDate timeIntervalSinceNow]; //to work with positive and negative time difference if( tempDateInterval < 0 ){ tempDateInterval *= -1; } NSInteger closestDateInterval = [closestDate timeIntervalSinceNow]; if( closestDateInterval < 0 ){ closestDateInterval *= -1; } if( tempDateInterval < closestDateInterval ){ closestDate = tempDate; } } 
+1
source

I am sure that there are many ways to throw a cat, but my favorite skinning mechanism is to convert the date to an era so that you know that you are comparing oranges with oranges and just comparing dates in seconds. This also fixes the problem with time zones.

If you need to make sure that you choose the closest time to the date, you may need to check the date with the time 00:00:01 and 23:59:59 and use the smallest result.

Using

 long secondsToDate = [date timeIntervalSince1970]; 
0
source

based on Roland Keesum's answer, here's how it worked for me:

  NSDate *currentDate = [[objects objectAtIndex:0] valueForKey:@"date"]; int minIndex = 0; for (int i = 1; i < [objects count]; ++i) { NSDate *tempDate = [[objects objectAtIndex:i] valueForKey:@"date"]; NSInteger tempDateInterval = [tempDate timeIntervalSinceNow]; if (tempDateInterval < 0) tempDateInterval *= -1; NSInteger closestDateInterval = [currentDate timeIntervalSinceNow]; if (closestDateInterval < 0) closestDateInterval *= -1; if( tempDateInterval < closestDateInterval ){ currentDate = tempDate; minIndex = i; } } 

minIndex returns the index of the nearest date today in the objects array.

0
source

All Articles