Number of days between two NSDate objects

I'm trying to compare two days of NSDate objects, I first used NSDateComponents to calculate the difference between my two date objects, but it works correctly.

NSString *dateStr =@"2012-11-05 15:00:00"; NSDate *date = [dateFormat dateFromString:dateStr]; NSDateComponents *datesDiff = [calendar components: NSDayCalendarUnit fromDate: date toDate: [NSDate date] options: 0]; 

This will not solve my problem. Indeed, when date1 = 2012-11-05 23:00:00 and date2 = 2012-11-06 10:00:00, the difference between the tow dates is 0 days and 11 hours. I am looking for something that allows me to detect changes in the day, in other words, when date1 = 2012-11-05 23:00:00 and date2 = 2012-11-06 00:01:00, there is one day diffrence.

If anyone knows a solution for this, I would welcome his suggestion. thanks in advance

+7
ios nsdate
Nov 05
source share
4 answers

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

Listing 13 days between two dates since the amount of midnight 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: Quick 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 } } 
+19
Nov 05 '12 at 18:18
source share
 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]; 

it matters now.

 NSLog(@"%ld", [components day]); 
+6
Jul 04 '13 at 7:55
source share
 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
Jul 01 '16 at 6:39
source share

For Swift 3:

@Masa answer extension:

 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
Mar 09 '17 at 15:38
source share



All Articles