gpt4 book ai didi

ios - 如何向 NSDate 添加周数?

转载 作者:可可西里 更新时间:2023-11-01 03:23:51 31 4
gpt4 key购买 nike

我过去曾使用以下函数使用 NSDateComponents 将特定时间间隔添加到现有日期。

(NSDate *)dateByAddingComponents:(NSDateComponents *)comps
toDate:(NSDate *)date
options:(NSCalendarOptions)opts

从 iOS8 开始,NSDateComponents 的周值已弃用,这意味着我无法实现我想做的事情:通过添加一定数量的几周到给定的 NSDate

如有任何帮助,我们将不胜感激。

最佳答案

只需使用weekOfYear:

Apple 文档 NSDateComponents week:

Deprecation Statement
Use weekOfYear or weekOfMonth instead, depending on what you intend.

NSDate *date = [NSDate date];
NSDateComponents *comp = [NSDateComponents new];
comp.weekOfYear = 3;
NSDate *date1 = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:date options:0];
NSLog(@"date: %@", date);
NSLog(@"date1: %@", date1);

输出:

     date:  2015-01-13 04:06:26 +0000  date1: 2015-02-03 04:06:26 +0000

如果您使用 week,您会收到此警告:

'week' is deprecated: first deprecated in ... - Use weekOfMonth or weekOfYear, depending on which you mean

当使用 weekOfMonthweekOfYear 作为增量时,它们的工作方式相同。它们的不同之处在于,当它们用于获取周数时,您将获得范围为 6 的月中的周数或范围为 53 的年中的周数。

关于ios - 如何向 NSDate 添加周数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27914241/

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