gpt4 book ai didi

ios - 如何从本周的工作日名称中获取特定日期

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:04:35 25 4
gpt4 key购买 nike

我们如何使用日期名称(星期一和星期日之间)获取当前星期中特定日期的日期?例如我想知道这个星期天是几号或上星期一是几号。

编辑:这是我如何借助@juniperi 的链接解决问题

//To get current day
CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 currentWeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);

//We are adding or substracting from the day we want and add to the current date
NSDate *now = [NSDate date];
int daysToAdd = theDayNumberWeWant - currentWeekdayNumber;
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];

最佳答案

int yourDOW = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit
fromDate:yourDate] weekday];

通过这种方式,您可以获得当前日期是哪一天,例如:星期一、星期二等。

据此,您可以计算下一天/前一天和/或日期:

例如:

(Sunday is represented with 1.) 20 Jun 2014 is Friday then result is 6. Then it means last Monday is: date - (currentDay - requiredDay) = 20 - ( 6 (friday) - 2 (monday)) = 20 - (4) = 16

It means, 16 Jun 2014 is the last Monday.

希望这对您有所帮助。有关详细信息,请参阅 NSDateComponents.

关于ios - 如何从本周的工作日名称中获取特定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24321771/

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