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 开发者_如何学JAVAi 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]);
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;
By only specifying the NSDayCalendarUnit
flag when creating the date components, you will get the number of days as result of [breakdownInfo day]
.
精彩评论