gpt4 book ai didi

iPhone SDK,如何获取即将到来的星期五20的NSDate对象:00?

转载 作者:行者123 更新时间:2023-12-03 16:50:55 25 4
gpt4 key购买 nike

有谁知道如何获取下周五 20:00 的 NSDate 吗?

最佳答案

是的,this article教你如何获取本周的星期日。

我很快将其调整为周五 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];

关于iPhone SDK,如何获取即将到来的星期五20的NSDate对象:00?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3870001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com