作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 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];
}
最佳答案
为了完整起见,这是我的最终解决方案(感谢 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];
}
关于cocoa - 如何根据给定的 NSDate 生成方便的日期范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1330798/
在javascript中,我可以这样写 console.log( person.name || person.nickname || '-' ) 然后会先查找name字段,如果找不到,会回退到nick
在为通用接口(interface)编写回调时,定义他们自己负责创建和访问的本地数据可能很有用。 在 C 中,我只使用一个空指针,类 C 示例: struct SomeTool { int ty
我是一名优秀的程序员,十分优秀!