如何使用 NSCalendar 或 NSDate 生成 30 天的数组?

发布于 2024-10-18 04:16:21 字数 601 浏览 2 评论 0原文

我正在尝试创建一个 30 天的数组,考虑到夏令时、闰年等。我目前有一个生成器,可以生成一系列天数,但它没有考虑特殊的时间变化和年份,月份变化。这是我当前的代码:

    NSMutableArray* dates = [[NSMutableArray alloc] init];
    int numberOfDays=30;
    NSDate *startDate=[NSDate date];
    NSDate *tempDate=[startDate copy];
    for (int i=0;i<numberOfDays;i++) {
        NSLog(@"%@",tempDate.description);
        tempDate=[tempDate dateByAddingTimeInterval:(60*60*24)];
        [dates addObject:tempDate.description];
    }

    NSLog(@"%@",dates);

创建一个生成器来循环日历以检索从今天的日期开始的接下来 30 天的最佳方法是什么,并且数组应包含今天的日期和接下来的 29 天。我当前的代码就像我说的那样工作,但并不完全准确。谢谢

I'm trying to create an array of 30 days that takes in consideration of daylight savings time, leap year, and etc. I currently have a generator that makes an array of days but It's not taking in consideration of special time changes and year, month changes. Here's my current code:

    NSMutableArray* dates = [[NSMutableArray alloc] init];
    int numberOfDays=30;
    NSDate *startDate=[NSDate date];
    NSDate *tempDate=[startDate copy];
    for (int i=0;i<numberOfDays;i++) {
        NSLog(@"%@",tempDate.description);
        tempDate=[tempDate dateByAddingTimeInterval:(60*60*24)];
        [dates addObject:tempDate.description];
    }

    NSLog(@"%@",dates);

What's the best way to create a generator to loop through the calendar to retrieve the next 30 days starting from today's date and the array should include today's date and the next 29 days. My current code works like I said but it's not totally accurate. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

云朵有点甜 2024-10-25 04:16:21

你差不多已经明白了;只需进行一些修改:

int numberOfDays=30;

NSDate *startDate=[NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *offset = [[NSDateComponents alloc] init];
NSMutableArray* dates = [NSMutableArray arrayWithObject:startDate];

for (int i = 1; i < numberOfDays; i++) {
  [offset setDay:i];
  NSDate *nextDay = [calendar dateByAddingComponents:offset toDate:startDate options:0];
  [dates addObject:nextDay];
}
[offset release];

NSLog(@"%@",dates);

这将创建一个 NSDate 对象数组。在我的机器上,此日志记录:

EmptyFoundation[4302:903] (
    "2011-02-16 16:16:26 -0800",
    "2011-02-17 16:16:26 -0800",
    "2011-02-18 16:16:26 -0800",
    "2011-02-19 16:16:26 -0800",
    "2011-02-20 16:16:26 -0800",
    "2011-02-21 16:16:26 -0800",
    "2011-02-22 16:16:26 -0800",
    "2011-02-23 16:16:26 -0800",
    "2011-02-24 16:16:26 -0800",
    "2011-02-25 16:16:26 -0800",
    "2011-02-26 16:16:26 -0800",
    "2011-02-27 16:16:26 -0800",
    "2011-02-28 16:16:26 -0800",
    "2011-03-01 16:16:26 -0800",
    "2011-03-02 16:16:26 -0800",
    "2011-03-03 16:16:26 -0800",
    "2011-03-04 16:16:26 -0800",
    "2011-03-05 16:16:26 -0800",
    "2011-03-06 16:16:26 -0800",
    "2011-03-07 16:16:26 -0800",
    "2011-03-08 16:16:26 -0800",
    "2011-03-09 16:16:26 -0800",
    "2011-03-10 16:16:26 -0800",
    "2011-03-11 16:16:26 -0800",
    "2011-03-12 16:16:26 -0800",
    "2011-03-13 16:16:26 -0700",
    "2011-03-14 16:16:26 -0700",
    "2011-03-15 16:16:26 -0700",
    "2011-03-16 16:16:26 -0700",
    "2011-03-17 16:16:26 -0700"
)

请注意 3 月 13 日时区偏移量如何从 -0800 更改为 -0700。那是夏令时。 :)

You've almost got it; just a couple modifications:

int numberOfDays=30;

NSDate *startDate=[NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *offset = [[NSDateComponents alloc] init];
NSMutableArray* dates = [NSMutableArray arrayWithObject:startDate];

for (int i = 1; i < numberOfDays; i++) {
  [offset setDay:i];
  NSDate *nextDay = [calendar dateByAddingComponents:offset toDate:startDate options:0];
  [dates addObject:nextDay];
}
[offset release];

NSLog(@"%@",dates);

This will create an array of NSDate objects. On my machine, this logs:

EmptyFoundation[4302:903] (
    "2011-02-16 16:16:26 -0800",
    "2011-02-17 16:16:26 -0800",
    "2011-02-18 16:16:26 -0800",
    "2011-02-19 16:16:26 -0800",
    "2011-02-20 16:16:26 -0800",
    "2011-02-21 16:16:26 -0800",
    "2011-02-22 16:16:26 -0800",
    "2011-02-23 16:16:26 -0800",
    "2011-02-24 16:16:26 -0800",
    "2011-02-25 16:16:26 -0800",
    "2011-02-26 16:16:26 -0800",
    "2011-02-27 16:16:26 -0800",
    "2011-02-28 16:16:26 -0800",
    "2011-03-01 16:16:26 -0800",
    "2011-03-02 16:16:26 -0800",
    "2011-03-03 16:16:26 -0800",
    "2011-03-04 16:16:26 -0800",
    "2011-03-05 16:16:26 -0800",
    "2011-03-06 16:16:26 -0800",
    "2011-03-07 16:16:26 -0800",
    "2011-03-08 16:16:26 -0800",
    "2011-03-09 16:16:26 -0800",
    "2011-03-10 16:16:26 -0800",
    "2011-03-11 16:16:26 -0800",
    "2011-03-12 16:16:26 -0800",
    "2011-03-13 16:16:26 -0700",
    "2011-03-14 16:16:26 -0700",
    "2011-03-15 16:16:26 -0700",
    "2011-03-16 16:16:26 -0700",
    "2011-03-17 16:16:26 -0700"
)

Note how the timezone offset changes on March 13th from -0800 to -0700. That's daylight savings time. :)

知足的幸福 2024-10-25 04:16:21

我上面的旁注的一些代码:

- (NSRange) daysInMonth:(NSDate*)date {

    NSCalendar* cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit) 
                                     fromDate:(date != nil) ? date: self.currentMonth];

    NSRange range = [cal rangeOfUnit:NSDayCalendarUnit
                              inUnit:NSMonthCalendarUnit
                             forDate:[cal dateFromComponents:comps]];

    return range;
}

some code for my sidenote above:

- (NSRange) daysInMonth:(NSDate*)date {

    NSCalendar* cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit) 
                                     fromDate:(date != nil) ? date: self.currentMonth];

    NSRange range = [cal rangeOfUnit:NSDayCalendarUnit
                              inUnit:NSMonthCalendarUnit
                             forDate:[cal dateFromComponents:comps]];

    return range;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文