Fabrizio
Fabrizio

Reputation: 183

Difference dates: total days

    NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
                                            fromDate:dateBegin
                                              toDate:dateEnd options:0];
totalDays = [components day];

That not gives me the total days between the 2 dates but simple the difference between the 2 days of the 2 dates. it dateBegin is 8 sep 2012 and dateEnd is 10 nov 2012 the result is 2! Why ?

Upvotes: 0

Views: 107

Answers (2)

PJR
PJR

Reputation: 13180

Change your bellow line

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

to

NSUInteger unitFlags = NSDayCalendarUnit;

Upvotes: 2

Fabrizio
Fabrizio

Reputation: 183

    NSDateComponents *components;
    NSInteger days;

    components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit 
    fromDate: startDate toDate: endDate options: 0];
    days = [components day];

I've found that solution too.

Upvotes: 0

Related Questions