2011-10-04 100 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); 
相关问题