5

我正在 Cocoa 中创建报告生成器,我需要生成方便的日期范围,例如“今天”、“本周”、“本月”、“今年”等。

有没有好的方法来做到这一点?到目前为止,这是我的骨架:

@interface DateRange : NSObject
{
    NSDate startDate;
    NSDate endDate;
}

@property (nonatomic, retain) NSDate * startDate;
@property (nonatomic, retain) NSDate * endDate;

+ (DateRange *)rangeForDayContainingDate:(NSDate *)date;
+ (DateRange *)rangeForWeekContainingDate:(NSDate *)date;
+ (DateRange *)rangeForMonthContainingDate:(NSDate *)date;
+ (DateRange *)rangeForYearContainingDate:(NSDate *)date;

@end

一些示例用例如下:

DateRange * thisWeek = [DateRange rangeForWeekContainingDate:[NSDate date]];
DateRange * thisYear = [DateRange rangeForYearContainingDate:[NSDate date]];

本质上,我希望返回的DateRange对象包含目标日期周围的周、月或年的开始和结束日期。例如(在伪代码中):

NSDate * today = [August 25, 2009];
DateRange * thisWeek = [DateRange rangeForWeekContainingDate:today];
assert(thisWeek.startDate == [August 23, 3009]);
assert(thisWeek.endDate == [August 29, 3009]);

更新:

感谢Kendall Helmstetter Geln 提供的答案,我能够完成这项工作。这是一周范围的完整方法:

+ (DateRange *)rangeForWeekContainingDate:(NSDate *)date
{
    DateRange * range = [[self alloc] init];

    // start of the week
    NSDate * firstDay;
    [[self calendar] rangeOfUnit:NSWeekCalendarUnit
                       startDate:&firstDay
                        interval:0
                         forDate:date];
    [range setStartDate:firstDay];

    // end of the week
    NSDateComponents * oneWeek = [[NSDateComponents alloc] init];
    [oneWeek setWeek:1];
    [range setEndDate:[[self calendar] dateByAddingComponents:oneWeek
                                                       toDate:firstDay
                                                      options:0]];
    [oneWeek release];

    return [range autorelease];
}
4

2 回答 2

8

为了完整起见,这是我的最终解决方案(感谢 Kendall Helmstetter Geln 和 jbrennan):

+ (NSCalendar *)calendar
{
    NSCalendar * gregorian = [[NSCalendar alloc]
                              initWithCalendarIdentifier:NSGregorianCalendar];
    return [gregorian autorelease];
}

////////////////////////////////////////////////////////////////

+ (NSDateComponents *)singleComponentOfUnit:(NSCalendarUnit)unit
{
    NSDateComponents * component = [[NSDateComponents alloc] init];

    switch (unit)
    {
        case NSDayCalendarUnit: [component setDay:1]; break;
        case NSWeekCalendarUnit: [component setWeek:1]; break;
        case NSMonthCalendarUnit: [component setMonth:1]; break;
        case NSYearCalendarUnit: [component setYear:1]; break;
    }

    return [component autorelease];
}

////////////////////////////////////////////////////////////////

+ (WM_DateRange *)rangeForUnit:(NSCalendarUnit)unit
               surroundingDate:(NSDate *)date
{
    WM_DateRange * range = [[self alloc] init];

    // start of the period
    NSDate * firstDay;
    [[self calendar] rangeOfUnit:unit
                       startDate:&firstDay
                        interval:0
                         forDate:date];
    [range setStartDate:firstDay];

    // end of the period
    [range setEndDate:[[self calendar]
        dateByAddingComponents:[self singleComponentOfUnit:unit]
                        toDate:firstDay
                       options:0]];

    return [range autorelease];
}

////////////////////////////////////////////////////////////////

+ (WM_DateRange *)rangeForDayContainingDate:(NSDate *)date
{ return [self rangeForUnit:NSDayCalendarUnit surroundingDate:date]; }

+ (WM_DateRange *)rangeForWeekContainingDate:(NSDate *)date
{ return [self rangeForUnit:NSWeekCalendarUnit surroundingDate:date]; }

+ (WM_DateRange *)rangeForMonthContainingDate:(NSDate *)date
{ return [self rangeForUnit:NSMonthCalendarUnit surroundingDate:date]; }

+ (WM_DateRange *)rangeForYearContainingDate:(NSDate *)date
{ return [self rangeForUnit:NSYearCalendarUnit surroundingDate:date]; }

////////////////////////////////////////////////////////////////

- (void)dealloc
{
    [endDate release];
    [startDate release];
    [super dealloc];
}
于 2009-08-26T00:21:48.557 回答
2

好吧,因为 timeInterval 以秒为单位,只需计算一下一天中有多少秒:

60 秒 * 60 分钟 * 24 小时 = 1 天。

然后在您的rangeForDayContainingDate方法中,您可以提取日期组件,获取当前日期,基于将小时和分钟设置为 0:00 的日期创建一个新日期,然后创建结束日期,添加上面计算的时间间隔。

于 2009-08-25T20:35:13.993 回答