1 +(int)compareDate:(NSDate *)date1 date:(NSDate *)date2
2 {
3 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
4 [dateFormatter setDateFormat:@"yyyy-MM-dd"];
5 //比较准确度为“日”,如果提高比较准确度,可以在此修改时间格式
6 NSString *stringDate1 = [dateFormatter stringFromDate:date1];
7 NSString *stringDate2 = [dateFormatter stringFromDate:date2];
8 NSDate *dateA = [dateFormatter dateFromString:stringDate1];
9 NSDate *dateB = [dateFormatter dateFromString:stringDate2];
10 NSComparisonResult result = [dateA compare:dateB];
11 if (result == NSOrderedDescending) {
12 return 1; //date1 比 date2 晚
13 } else if (result == NSOrderedAscending){
14 return -1; //date1 比 date2 早
15 }
16 return 0; //在当前准确度下,两个时间一致
17 }