2011-10-04 101 views
1

我設計了一個應用程序,用於發送推送通知。該通知基於時間間隔生成。該時間間隔由當前時間和次日選定時間的差異來計算。所以我遇到了一個問題,那就是我如何設定下一天的時間,我想要設置?第二個問題是如何獲得它們之間的差異?如何獲得當前時間和次日設定時間之間的時差?

回答

1

下面是代碼來獲取時間間隔的時間明天:

NSCalendar *calendar = [NSCalendar currentCalendar]; 

// Get today date 
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]]; 

// Set the time tomorrow 
components.hour = 12; 
components.minute = 30; 
components.day = components.day + 1; 

NSDate *tomorrow = [calendar dateFromComponents:components]; 
NSLog(@"tomorrow: %@", tomorrow); 

NSTimeInterval timeTillTomorrow = [tomorrow timeIntervalSinceNow]; 
NSLog(@"timeTillTomorrow: %.0f seconds", timeTillTomorrow); 
相關問題