Как я могу сравнить две даты, возвратите много дней

как я могу выдержать сравнение, две даты возвращают количество дней. Исключая: Пропавшие без вести X дней Кубка. посмотрите мой код.

  NSDateFormatter *df = [[NSDateFormatter alloc]init];  
  [df setDateFormat:@"d MMMM,yyyy"];  
  NSDate *date1 = [df dateFromString:@"11-05-2010"];  
  NSDate *date2 = [df dateFromString:@"11-06-2010"];  
  NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];  
  //int days = (int)interval / 30;  
  //int months = (interval - (months/30)) / 30;  
  NSString *timeDiff = [NSString stringWithFormat:@"%dMissing%d days of the Cup",date1,date2, fabs(interval)];  

  label.text = timeDiff; // output (Missing X days of the Cup)  
22
задан Dour High Arch 30 March 2010 в 19:23
поделиться

1 ответ

Из примера Apple , в основном используйте NSCalendar:

NSDate * date1 = <however you initialize this>;
NSDate * date2 = <...>;

NSCalendar *gregorian = [[NSCalendar alloc]
                 initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
                                          fromDate:date1
                                          toDate:date2 options:0];

NSInteger months = [components month];
NSInteger days = [components day];
35
ответ дан 29 November 2019 в 04:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: