我正在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]);
更新
感谢answer provided by 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];
}
答案 0 :(得分: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];
}
答案 1 :(得分:2)
因为timeInterval以秒为单位,所以只需计算出一天中的秒数:
60秒* 60分钟* 24小时= 1天。
然后在您的rangeForDayContainingDate
方法中,您可以提取日期组件,获取当前日期,根据设置为0:00的小时和分钟的日期创建新日期,并创建添加时间的结束日期上面计算的区间。