Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.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
iphonesdk,如何获取即将到来的周五&x27;20点?_Iphone_Cocoa_Ios_Nsdate_Nscalendar - Fatal编程技术网

iphonesdk,如何获取即将到来的周五&x27;20点?

iphonesdk,如何获取即将到来的周五&x27;20点?,iphone,cocoa,ios,nsdate,nscalendar,Iphone,Cocoa,Ios,Nsdate,Nscalendar,有人知道如何得到即将到来的星期五20:00的日期吗 是的,教你如何获得本周的星期日 我很快将它调整为20:00的星期五(假设是公历) 是的,教你如何获得本周的星期日 我很快将它调整为20:00的星期五(假设是公历) NSDate *today = [[NSDate alloc] init]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // Get th

有人知道如何得到即将到来的星期五20:00的日期吗

是的,教你如何获得本周的星期日

我很快将它调整为20:00的星期五(假设是公历)

是的,教你如何获得本周的星期日

我很快将它调整为20:00的星期五(假设是公历)

NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];

/*
Create a date components to represent the number of days to add to the current date.
The weekday value for Friday in the Gregorian calendar is 6, so add the difference between 6 and today to get the number of days to add.
Actually, on Saturday that will give you -1, so you want to subtract from 13 then take modulo 7
*/
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
[componentsToAdd setDay: (13 - [weekdayComponents weekday]) % 7];

NSDate *friday = [gregorian dateByAddingComponents:componentsToAdd toDate:today options:0];

/*
friday now has the same hour, minute, and second as the original date (today).
To normalize to midnight, extract the year, month, and day components and create a new date from those components.
*/
NSDateComponents *components =
    [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
               fromDate: friday];

// And to set the time to 20:00
[components setHour:22];

friday = [gregorian dateFromComponents:components];