今から年末までの日付の差を取得しようとすると、その月に 0 が返されます。オブジェクトを使用して、NSCalendar
2 つのオブジェクトの違いを取得してい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;