Iphone 如何获取当前时间和第二天设置时间之间的时差?
我设计了一个应用程序,在其中发送推送通知。该通知根据时间间隔生成。该时间间隔根据当前时间和第二天选定时间的差值计算。所以我遇到了一个问题,那就是我如何为接下来的几天设定我想要设定的时间?第二个问题是如何获得它们之间的差异?下面是获取时间间隔到明天某个时间的代码:Iphone 如何获取当前时间和第二天设置时间之间的时差?,iphone,nsdateformatter,Iphone,Nsdateformatter,我设计了一个应用程序,在其中发送推送通知。该通知根据时间间隔生成。该时间间隔根据当前时间和第二天选定时间的差值计算。所以我遇到了一个问题,那就是我如何为接下来的几天设定我想要设定的时间?第二个问题是如何获得它们之间的差异?下面是获取时间间隔到明天某个时间的代码: NSCalendar *calendar = [NSCalendar currentCalendar]; // Get today date NSDateComponents *components = [calendar compo
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);
下面是将时间间隔设置为明天某个时间的代码:
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);