Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何获取当前时间和第二天设置时间之间的时差?_Iphone_Nsdateformatter - Fatal编程技术网

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);