0

这是设置:

NSDateComponents* temporalComponents;

temporalComponents = [theDateComponents copy];  //another component set to todays date.

[temporalComponents setDay:   [temporalComponents day] + (offset * 7)]

如果我输出组件的结果,则表明正确添加了天数,例如 21 天。

"<NSDateComponents: 0x6a48820>\n    Calendar Year: 2012\n    Month: 2\n    Day: 49\n    Hour: 13\n    Minute: 12"

但当然我希望它有这个更正的“包装”组件列表,其中月份增加并且日期被更正..(主要是因为我做了进一步的操作,我可以替换整个组件,而不仅仅是添加到组件)换句话说,我的最终产品是 NSDateComponent,因此可以进一步对其进行操作。

"<NSDateComponents: 0x6a48820>\n    Calendar Year: 2012\n    Month: 3\n    Day: 20\n    Hour: 13\n    Minute: 12"

现在我这样做是为了正确地包裹日子(以及其他需要包裹的东西)

NSDate* tempDate = [CURRENTC dateFromComponents:temporalComponents];

temporalComponents2 = [CURRENTC components:unitFlags fromDate:tempDate];

[theDatesArray addObject: temporalComponents2];

有没有更好的方法?无需去约会,然后回到组件。我一直在寻找更好的方法但没有成功,也许我在搜索错误的术语。

4

1 回答 1

0

The provided means for adding a given offset to a given date is to use NSCalendar's dateByAddingComponents:toDate:options:. So you'd do something like this:

NSDateComponents *daysToAdd = [[[NSDateComponents alloc] init] autorelease];
daysToAdd.day = 23;

NSDate *derivedData =
      [CURRENTC dateByAddingComponents:daysToAdd
                toDate:sourceDate
                options:NSWrapCalendarComponents];

With the implicit assumption being that you generally store things as NSDate's, with NSDateComponents being the helper that allows you to manipulate them relative to a given NSCalendar.

So in your case I don't think you can do any better than you're doing, because you're using what Apple consider the helper as the actual storage.

于 2012-02-28T20:57:34.853 回答