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]);
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;
通过仅在创建日期组件时指定NSDayCalendarUnit
标志,您将获得[breakdownInfo day]
结果的[breakdownInfo day]
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.