tags:

views:

72

answers:

2

hi, 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]);
A: 

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

Rhult
it gives wrong out put...month returns 2.. but days return only 13 after i implemented your Way... t
Mikhail Naimy
I think you are doing something wrong then. If you only set the day unit, only the day field will contain a valid value. The other fields are just random so you can't use them. When I just set the day unit, comparing two days that are one month apart, I get for example 30 in the day field.
Rhult
+1  A: 

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;
Darren