user141302
user141302

Reputation:

to change months to exact days?

i am using following code to get months correctly....it returns correct data..but it gives 2 months when i get from two dates...suppose if i give two dates like janauary to march, it will give 2 months...how can i change to no of days exactly(included feb)...

NSCalendar *sysCalendar = [NSCalendar currentCalendar];


unsigned int unitFlags = NSSecondCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;

NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];

NSLog(@"Break down: %dmons", [breakdownInfo month]);

Upvotes: 0

Views: 148

Answers (2)

Darren
Darren

Reputation: 25619

You could use NSDateComponents, but you can count days simply by taking the difference of each date's timeInterval. NSTimeInterval is a floating point number that counts the number of seconds.

#define kSecondsInDay 86400

int numberOfDays = [date2 timeIntervalSinceDate:date1] / kSecondsInDay;

Upvotes: 0

Rhult
Rhult

Reputation: 1666

By only specifying the NSDayCalendarUnit flag when creating the date components, you will get the number of days as result of [breakdownInfo day].

Upvotes: 1

Related Questions