您最初无法比较两个日期字符串,您必须使用以下代码将该字符串转换为两个日期
// The current date :2013-06-27 13:51
// The server date :2013-06-27 11:51
NSString *serverDate = @"2013-06-27 13:51";
NSDateFormatter *datePickerFormat = [[NSDateFormatter alloc] init];
[datePickerFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[datePickerFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *convertedDate = [datePickerFormat dateFromString:serverDate];
NSLog(@"date %@",convertedDate);
转换后,您可以使用@prince 所说的以下代码进行比较
NSDate *today = [NSDate date]; // current date
NSDate *newDate = self.serverDate; // other date
NSComparisonResult result;
result = [today compare:newDate]; // comparing two dates
if(result == NSOrderedAscending)
NSLog(@"today is less");
else if(result == NSOrderedDescending)
NSLog(@"newDate is less");
else if(result == NSOrderedSame)
NSLog(@"Both dates are same");
else
NSLog(@"Date cannot be compared");