我做了一个小程序来计算两个日期之间的差异,并输出经过的天数和月数。该程序分别显示月数和罚款天数。但是,当我将它们组合成一个标志时,无论我输入什么日期,输出都只显示正确的月数和天数“12”。我也尝试过 inline components: (NSDayCalendarUnit|NSMonthCalendarUnit
),但是出现同样的问题。这是我的代码。非常感谢您的帮助!
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSDate *startDate=[NSDate dateWithString:@"2004-2-24 1:54:10 +0600"];
NSDate *today=[NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger flags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [gregorian components: flags fromDate:startDate toDate:today options:0];
NSInteger days = [components day];
NSInteger months = [components month];
NSLog(@"%li",(long)days);
NSLog(@"%li",(long)months);
}
return 0;
}
输出:
2013-05-05 14:49:36.943 calenderCalc[4111:303] 12
2013-05-05 14:49:36.944 calenderCalc[4111:303] 110