Compare dos dates

¿Cómo puedo comparar dos dates diferentes para averiguar cuál es la date posterior? Por ejemplo, en date1 una date después de download algunos datos, y en date2 , date2 la date actual. Entonces necesito verificar cuál es mayor / más adelante: algo así como if(date1>date2) .

Algo como:

  NSDate* timeNow = [NSDate date]; // If less than 30 seconds, do something if ([timeNow timeIntervalSinceDate:anEarlierTime] < 30.0f) { // Do something } 

puede usar comparar NSDate: método:

 NSComparisonResult result = [Date2 compare:Date1]; if(result==NSOrdenetworkingAscending) NSLog(@"Date1 is in the future"); else if(result==NSOrdenetworkingDescending) NSLog(@"Date1 is in the past"); else NSLog(@"Both dates are the same"); 
 NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd"]; NSDate *dt1 = [[NSDate alloc] init]; NSDate *dt2 = [[NSDate alloc] init]; dt1 = [df dateFromString:@"2011-02-25"]; dt2 = [df dateFromString:@"2011-03-25"]; NSComparisonResult result = [dt1 compare:dt2]; switch (result) { case NSOrdenetworkingAscending: NSLog(@"%@ is greater than %@", dt2, dt1); break; case NSOrdenetworkingDescending: NSLog(@"%@ is less %@", dt2, dt1); break; case NSOrdenetworkingSame: NSLog(@"%@ is equal to %@", dt2, dt1); break; default: NSLog(@"erorr dates %@, %@", dt2, dt1); break; } 

Esto es lo que utilicé para comparar dates con zonas horarias, y funcionó para mí usando iOS 6

  NSComparisonResult result = [timeStamp compare:serverDate]; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd HH:mm:ss z"]; switch (result) { case NSOrdenetworkingAscending: NSLog(@"%@ is greater than %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]); break; case NSOrdenetworkingDescending: NSLog(@"%@ is less %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]); break; case NSOrdenetworkingSame: NSLog(@"%@ is equal to %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]); break; default: NSLog(@"erorr dates %@, %@", [df stringFromDate:serverDate], [df stringFromDate:timeStamp]); break; }