使用 NSCalendar 创建 NSMutableArray

发布于 2024-09-29 18:08:17 字数 268 浏览 5 评论 0原文

我有以下问题:我需要在特定日期之后的每个工作日创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-10-06 18:08:17

这是一个无穷级数; 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.

两人的回忆 2024-10-06 18:08:17

要获得实际日期:

NSDate *today = [[NSDate alloc] init];

要添加一周:

NSDate *nextDate = [today dateByAddingTimeInterval:60*60*24*7];

然后您可以迭代并创建数组:

NSMutableArray* dates = [[NSMutableArray alloc] init];
NSDate *date= [[NSDate alloc] init];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setWeek:1];

for (int i=0;i<10;i++) {
  NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:date options:0];
  [dates addObject:date];
  [date release];
  date = nextDate;
}
[date release];

To have actual date:

NSDate *today = [[NSDate alloc] init];

To add a week:

NSDate *nextDate = [today dateByAddingTimeInterval:60*60*24*7];

Then you can iterate and create your array:

NSMutableArray* dates = [[NSMutableArray alloc] init];
NSDate *date= [[NSDate alloc] init];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setWeek:1];

for (int i=0;i<10;i++) {
  NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:date options:0];
  [dates addObject:date];
  [date release];
  date = nextDate;
}
[date release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文