2011-04-04 48 views
0

我試圖將nsdate增加1個月,但是當它到達年底時,年不會更新(從第一個月開始)?這怎麼解決?NSDate increment

這是我用什麼代碼

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
NSDateComponents *comp = [[[NSDateComponents alloc] init] autorelease]; 
[comp setMonth: 1]; 
NSDate* newDate = [gregorian dateByAddingComponents:comp toDate:oldDate options:NSWrapCalendarComponents]; 

回答

5

這就是NSWrapCalendarComponents選項的作用。而是通過0來獲得您要查找的行爲。

+0

感謝它的工作原理:D – user648244 2011-04-04 23:23:00