finding NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?

百般思念 提交于 2019-12-07 19:52:53

问题


Is there a quick way in Objective-C of identifying NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?

I can't quite see anyway other than manually walking through each NSDate in the array and then using NSDateComponents to break out the hour/minute/second...Not even sure if there is a simple way to get the time from an NSDate in a fashion that represents a fraction of 24hours, as this might help a little. (e.g. 6pm would be 18/24 = 0.75 in this case)


回答1:


There is no need to break in NSDateComponents.

NSTimeInterval interval = [date1 timeIntervalSinceDate:date2];
if (interval > 0) {
    // date2 is earlier
} else {
    // date1 is earlier
}

Now you can represent your target time(8 P.M., for example) with date2 and compare all dates of array with that.




回答2:


Haven't tried this myself, but I guess

- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate

is what you're looking for.



来源:https://stackoverflow.com/questions/5367502/finding-nsdates-in-an-nsarray-that-have-a-time-of-day-after-a-given-time-e-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!