How can I compare two dates, return a number of days

后端 未结 4 1399
生来不讨喜
生来不讨喜 2020-12-05 13:57

how can I compare two dates return number of days. Ex: Missing X days of the Cup. look my code.

  NSDateFormatter *df = [[NSDateFormatter alloc]init];  
  [d         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 14:37

    From Apple's example, basically use an NSCalendar:

    NSDate * date1 = ;
    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];
    

提交回复
热议问题