2011-03-07 105 views
2

我想工作了幾個月的差異鍛鍊區別

在現階段,我有這樣的代碼:

dateInterval = [endDate timeIntervalSinceDate:startDate]; 

但是,返回值(秒),我想查看幾個月內的日期之間的差異。

我該怎麼做?

感謝

回答

5
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init]; 
[inputFormatter setDateFormat:@"dd/MM/yyyy"]; 
NSDate *startDate = [inputFormatter dateFromString:@"07/03/2011"]; 
NSDate *endDate = [inputFormatter dateFromString:@"07/06/2011"]; 
NSInteger month_delta = [[[NSCalendar currentCalendar] components: NSMonthCalendarUnit fromDate: startDate toDate: endDate options: 0] month]; 
NSLog(@"---------------------------->>%d", month_delta); 
[inputFormatter release]; // <-- in case not using ARC 

它會記錄:

-------------------- -------- >> 3

+0

+1這是正確的做法。它甚至可以工作12個月以上的差異。 – 2011-03-07 18:25:25