sam*_*les 0 objective-c nsdate nsarray
我有这样的一系列时间:
(
"2000-01-01 23:48:00 +0000",
"2000-01-01 02:15:00 +0000",
"2000-01-01 04:39:00 +0000",
"2000-01-01 17:23:00 +0000",
"2000-01-01 13:02:00 +0000",
"2000-01-01 21:25:00 +0000"
)
Run Code Online (Sandbox Code Playgroud)
这是从这段代码生成的:
//loop through array and convert the string times to NSDates
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"hh:mm a"];
NSMutableArray *arrayOfDatesAsDates = [NSMutableArray array];
for (NSObject* o in arrayTimes)
{
NSLog(@"%@",o);
NSDate *nsDateTime = [timeFormatter dateFromString:o];
[arrayOfDatesAsDates addObject:nsDateTime];
}
NSLog(@"times array: %@", arrayOfDatesAsDates);//
Run Code Online (Sandbox Code Playgroud)
我这样做是因为我试图在接下来的数组中获得时间.我的计划是删除过去的时间,订购它们,然后在下次使用第一个.
我怎样才能删除过去的?
谢谢
像这样的东西会......
NSArray *dateArray = ...
NSArray *filteredArray = [dateArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSDate *date, NSDictionary *bind){
NSComparisonResult result = [[NSDate date] compare:date];
return result == NSOrderedAscending;
}]];
Run Code Online (Sandbox Code Playgroud)
filteredArray将是dateArray中现在或将来的所有日期.