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

后端 未结 4 1373
生来不讨喜
生来不讨喜 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:50

    You can use next category:

    @interface NSDate (Additions)
    
    -(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate; 
    -(NSInteger)numberOfHoursUntilDay:(NSDate *)aDate;
    
    @end
    
    @implementation NSDate (Additions)
    const NSInteger secondPerMunite = 60;       
    const NSInteger munitePerHour = 60;
    const NSInteger hourPerDay = 24;
    
    -(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate 
    {
        NSInteger selfTimeInterval = [aDate timeIntervalSinceDate:self];   
        return abs(selfTimeInterval / (secondPerMunite * munitePerHour * hourPerDay));    
    }
    
    -(NSInteger)numberOfHoursUntilDay:(NSDate *)aDate 
    {
        NSInteger selfTimeInterval = [aDate timeIntervalSinceDate:self];
        return abs(selfTimeInterval / (secondPerMunite * munitePerHour));
    }
    @end
    

提交回复
热议问题