我正在使用NSDateComponents和NSCalendar获取两个日期之间的日期。
// Get dates between before and after
NSDateComponents *betweenDateComponents = [calendar components:componentFlags
fromDate:afterDate
toDate:beforeDate
options:0];
然后我有一个简单的for循环,它基于组件的day属性:
for (int i = 1; i < betweenDateComponents.day; ++i) {
当我在一周或一个月的所有日子里工作时,这非常有效。如果我按周过滤,则day属性记录为6.如果我按月过滤day属性为29,但出于某种原因,当我按年过滤时,day属性记录为29而不是364。
为什么当我尝试工作一年时,白天的财产总是错误的?
这是我的所有代码,直到上面包含的for循环:
// Set the date components according to the type of filter we're doing
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [NSDateComponents new];
if (filterType == CommentFilterTypeWeeklyByDay) {
[components setDay:-6];
} else if (filterType == CommentFilterTypeMonthlyByDay) {
[components setDay:-29];
} else if (filterType == CommentFilterTypeYearlyByMonth) {
[components setDay:-364];
}
NSUInteger componentFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
// Zero out before and after dates
NSDate *beforeDate = [NSDate date];
NSDateComponents *zeroComponents = [[NSCalendar currentCalendar] components:componentFlags fromDate:beforeDate];
beforeDate = [calendar dateFromComponents:zeroComponents];
NSDate *afterDate = [calendar dateByAddingComponents:components toDate:beforeDate options:0];
zeroComponents = [[NSCalendar currentCalendar] components:componentFlags fromDate:afterDate];
afterDate = [calendar dateFromComponents:zeroComponents];
NSMutableArray *dates = [NSMutableArray new];
[dates addObject:afterDate];
// Get dates between before and after
NSDateComponents *betweenDateComponents = [calendar components:componentFlags
fromDate:afterDate
toDate:beforeDate
options:0];
NSLog(@"%d", betweenDateComponents.day);
for (int i = 1; i < betweenDateComponents.day; ++i) {
答案 0 :(得分:1)
由于您的组件包含NSYearCalendarUnit
和NSMonthCalendarUnit
,我怀疑您必须使用month
和year
而不是day
。这些成分是累积的,而不是独立的。
通过更改components
:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [NSDateComponents new];
switch (filterType) {
case CommentFilterTypeWeeklyByDay: [components setDay:-6]; break;
case CommentFilterTypeMonthlyByDay: [components setDay:-29]; break;
case CommentFilterTypeYearlyByMonth: [components setDay:-364]; break;
case CommentFilterTypeCustom: [components setDay:-590]; break;
default: break;
}
NSUInteger componentFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDate *beforeDate = [NSDate date];
NSDateComponents *zeroComponents = [[NSCalendar currentCalendar] components:componentFlags fromDate:beforeDate];
beforeDate = [calendar dateFromComponents:zeroComponents];
NSDate *afterDate = [calendar dateByAddingComponents:components toDate:beforeDate options:0];
zeroComponents = [[NSCalendar currentCalendar] components:componentFlags fromDate:afterDate];
afterDate = [calendar dateFromComponents:zeroComponents];
NSDateComponents *betweenDateComponents = [calendar components:NSDayCalendarUnit
fromDate:afterDate
toDate:beforeDate
options:0];
NSLog(@"%d", betweenDateComponents.day);
此处NSLog
打印
6
的 CommentFilterTypeWeeklyByDay
29
CommentFilterTypeMonthlyByDay
364
CommentFilterTypeYearlyByMonth
590
CommentFilterTypeCustom