当我尝试获取从现在到年底的日期差异时,我得到的月份为 0。我正在使用一个NSCalendar
对象来获取两个对象之间的差异NSDate
。
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:2013];
[comps setMonth:0];
[comps setDay:0];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *newDate = [calendar dateFromComponents:comps];
NSDateComponents *timeDifference = [[NSCalendar currentCalendar]
components: NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |
NSMinuteCalendarUnit | NSSecondCalendarUnit
fromDate:[NSDate date] toDate:newDate options:0];
int months = timeDifference.month;
int days = timeDifference.day;
if (months>0)
days+=31;
int hours = timeDifference.hour;
int minutes = timeDifference.minute;
int seconds = timeDifference.second;