使用 NSCalendar 创建 NSMutableArray
我有以下问题:我需要在特定日期之后的每个工作日创建一个 NSMutabeArray 。
这应该看起来像:
- 2010 年 10 月 28 日星期四
- 2010 年 10 月 4 日星期四 2010
- 年 10 月 11 日星期四
- ...
我该怎么做?我认为这与 NSCalendar 有关,但我找不到正确的解决方案......你能帮我吗?
预先感谢您
FFraenz
I have following problem: I need to create an NSMutabeArray with every weekday after a specific date.
This should look like:
- Thursday 28 october 2010
- Thursday 04 october 2010
- Thursday 11 october 2010
- ...
How can I do that? I think it has something to do with NSCalendar, but I can't find the right solution... Could you help me?
Thank you in advance
FFraenz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个无穷级数; NSMutableArray 只能保存有限的集合。
无论如何,您只需要该系列的一个成员,例如 2010-10-28。要获得此后的星期四,请添加一周。要获得该系列中的第三个日期,请在第二个日期上添加一周,或在第一个日期上添加两周。拥有该系列的任何成员都可以让您访问该系列的任何其他成员。
如果您从不正确的工作日开始,请获取该日期的日期部分,将正确的工作日与该日期的工作日之间的差值添加到该月的日期,然后将修改后的日期部分转换回约会。该日期将在同一周的所需工作日。
That's an infinite series; an NSMutableArray can only hold a finite collection.
At any rate, you need only a single member of the series, such as 2010-10-28. To get the Thursday after that, add one week. To get the third date in the series, add a week to the second date, or two weeks to the first date. Having any member of the series provides you with access to any other member of the series.
If you are starting from a date that isn't the right weekday, get the date components for that date, add the difference between the correct weekday and the weekday it has to its day of the month, and convert the amended date components back to a date. That date will then be on the desired weekday in the same week.
要获得实际日期:
要添加一周:
然后您可以迭代并创建数组:
To have actual date:
To add a week:
Then you can iterate and create your array: