Number of days between two NSDate objects

前端 未结 4 1409
悲&欢浪女
悲&欢浪女 2020-12-16 04:31

I\'m trying to compare two days of NSDate objects, I used at the first time NSDateComponents to calculate difference between my two dates Objects, but it is working correct

相关标签:
4条回答
  • 2020-12-16 04:55

    I think you are looking for this (from the Date and Time Programming Guide):

    Listing 13 Days between two dates, as the number of midnights between

    @implementation NSCalendar (MySpecialCalculations)
    
    -(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
    {
         NSInteger startDay=[self ordinalityOfUnit:NSDayCalendarUnit
              inUnit: NSEraCalendarUnit forDate:startDate];
         NSInteger endDay=[self ordinalityOfUnit:NSDayCalendarUnit
              inUnit: NSEraCalendarUnit forDate:endDate];
         return endDay-startDay;
    }
    @end
    

    EDIT: Swift version:

    extension NSCalendar {
        func daysWithinEraFromDate(startDate: NSDate, endDate: NSDate) -> Int {
            let startDay = self.ordinalityOfUnit(.Day, inUnit: NSCalendarUnit.Era, forDate: startDate)
            let endDay = self.ordinalityOfUnit(.Day, inUnit: NSCalendarUnit.Era, forDate: endDate)
            return endDay - startDay
        }
    }
    
    0 讨论(0)
  • 2020-12-16 04:55
    NSString *start = @"2010-09-01";
    NSString *end = @"2010-12-01";
    
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd"];
    NSDate *startDate = [f dateFromString:start];
    NSDate *endDate = [f dateFromString:end];
    [f release];
    
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                        fromDate:startDate
                                                          toDate:endDate
                                                         options:0];
    [gregorianCalendar release];
    

    components now holds the difference.

    NSLog(@"%ld", [components day]);
    
    0 讨论(0)
  • 2020-12-16 04:59
    func daysBetweenDates(startDate: NSDate, endDate: NSDate) -> Int
    {
        let calendar = NSCalendar.currentCalendar()
    
        let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])
    
        return components.day
    }
    

    or

    let calendar = NSCalendar.currentCalendar();
    let component1 = calendar.component(.Day, fromDate: fromDate)
    let component2 = calendar.component(.Day, fromDate: toDate)
    let difference  = component1 - component2
    
    0 讨论(0)
  • 2020-12-16 05:05

    For Swift 3:

    Expanding from @masa's answer:

    extension Calendar {
        func daysWithinEraFromDate(startDate: Date, endDate: Date) -> Int? {
            guard let startDay = ordinality(of: .day, in: .era, for: startDate) else {
                return nil
            }
    
            guard let endDay = ordinality(of: .day, in: .era, for: endDate) else {
                return nil
            }
    
            return endDay - startDay
        }
    }
    
    0 讨论(0)
提交回复
热议问题