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

后端 未结 4 1306
生来不讨喜
生来不讨喜 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 = <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];
    
    0 讨论(0)
  • 2020-12-05 14:49

    Need to call this method and set 2 dates only which we want to calculate differently.

    -(void) calculateSleepHours:(NSDate *)sleepDate :(NSDate *)wakeStr 
    {
    
       if (sleepDate !=nil && wakeStr!=nil) {`enter code here`
    
        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
        [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ssZ"];
        NSDate *date1 = [ApplicationManager getInstance].sleepTime;;
    
        NSDate *date2 = [ApplicationManager getInstance].wakeupTime;
    
        NSCalendar *gregorian = [[NSCalendar alloc]
                                 initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    
        NSDateComponents *components = [gregorian components:unitFlags
                                                    fromDate:date1
                                                      toDate:date2 options:0];
    
        NSInteger months = [components month];
        NSInteger days = [components day];
        NSInteger hours = [components hour];
        NSInteger minute=[components minute];
        NSInteger second=[components second];
        DLog(@"Month %ld day %ld hour is %ld min %ld sec %ld  ",(long)months,(long)days,(long)hours,(long)minute,(long)second);
    
        sleepHours.text=[NSString stringWithFormat:@"Hour %ld Min %ld Sec %ld",(long)hours,(long)minute,(long)second];
        }
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-05 15:04

    Try this category:

    @interface NSDate (DateUtils)
    
    -(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate; 
    
    @end
    
    @implementation NSDate (DateUtils)
    
    -(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate
    {
    
        NSCalendar *calendar = [[NSCalendar alloc]
                                 initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
        NSDateComponents *components = [calendar components:NSCalendarUnitDay
                                                    fromDate:self
                                                      toDate:aDate options:kNilOptions];
    
        return [components day];
    
    }
    
    @end
    

    You can use this category by adding an import:

    #import "NSDate+DateUtils.h"
    

    And call it from your code:

    NSInteger days = [myDate numberOfDaysUntilDay:someOtherDate];
    
    0 讨论(0)
提交回复
热议问题