Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have array with times like below

         "00:00",
         "01:25",
         "02:00",
         "02:35",
         "04:35",
         "05:00",
         "06:00",
         "07:00",
         "09:00",
         "16:30",
         "17:30",
         "18:00",
         "18:30",
         "19:30",
         "21:30",
         "22:00",
         "22:30",
         "23:00"

how get index of object which time near to current time?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can get the time difference like this

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinCalendarUnit) fromDate:today];

[components setHour:otherHour];
[components setMinute:otherMinute];

NSDate *otherDate = [gregorian dateFromComponents:components];

NSTimeInterval timeDifferenceBetweenDates = [today timeIntervalSinceDate:otherDate];

do that for all your hours/minutes and take the minimum absolute time interval.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...