2012-11-20 14 views
0

我需要能夠存儲當前NS日期加上7天。我已經存儲了當前的NSDate。然後我想比較這兩個日期,以獲得兩者之間的天數差並顯示在標籤內。這個想法是從7天倒計時到6天到5天等,直到它顯示0,我會從我的桌面視圖中刪除它。如何存儲NSDate加上7個日曆日?

我一直很難找到這個寫代碼,所以任何幫助將不勝感激!

回答

0

此代碼將計算從今天起7天后的日期。

NSDateComponents *comps = [NSDateComponents new]; 
comps.day = 7; 

NSDate *today = [NSDate date]; 

NSCalendar *c = [NSCalendar currentCalendar]; 
NSDate *dateAfter7days = [c dateByAddingComponents:comps toDate:today options:0]; 

對於你的約會diffrence代碼: -

// c is calendar object 
    NSDateComponents *comps = [c components:NSDayCalendarUnit fromDate:startDate toDate:currentDate options:0]; 
    int diffrence = [comps day]; 
+0

謝謝!它完美的作品! – mreynol

+0

其實,日期加上7天完美工作,但我有日期差異的問題。我得到了警告:不兼容的整數轉換指針初始化'int'的類型爲'NSDateComponents'的表達式 – mreynol

+0

@mreynol感謝您指出我的錯誤我編輯了代碼 –