2 NSDateComponents - is it same week?
i have got a function with BOOL return value and 2 input (NSDateComponents *) parameters. My trouble is I have two NSDateComponents values and I want to know if the two date fall within the same calendar week. i tried the simplest solutions to solve problem, my idea was the following:
- (BOOL)isFunctionName:(NSDateComponents *)comp1 andParam:(NSDateComponents *)comp2 {
return (([comp1 week] == [comp2 week]) && ([comp1 year] 开发者_运维技巧== [comp2 year]));
}
but it's not correct. what way i can solve it ?
edited
so i have a function which makes datecomponents from dates.
-(NSDateComponents *)dateToDateComponents:(NSDate *)date {
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:unitFlags fromDate:date];
[gregorian release];
return dateComponents;
}
and i call it this way:
if ([self isFunctionName: [self dateToDateComponents:startDate] and Param:[self dateToDateComponents:currentTripDate]]){
}
and during my test it returns YES all of my dates (for example 2010.07.21 - 2010.08.18)
This is inspired by Robert's answer. I tweaked it a bit to address the issue when a week crosses two years, e.g. 12/31/2012 & 1/1/2013.
- (BOOL)isOnSameWeekAsDate:(NSDate*)otherDate
{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
unsigned unitFlags = NSCalendarUnitYearForWeekOfYear | NSCalendarUnitWeekOfYear;
NSDateComponents *components = [calendar components:unitFlags fromDate:self];
NSDateComponents *otherComponents = [calendar components:unitFlags fromDate:otherDate];
return ( [components yearForWeekOfYear] == [otherComponents yearForWeekOfYear] && [components weekOfYear] == [otherComponents weekOfYear] );
}
The NSDateComponent class reference states:
Important: An
NSDateComponents
object is meaningless in itself; you need to know what calendar it is interpreted against, and you need to know whether the values are absolute values of the units, or quantities of the units.
What about comparing NSDate
s instead?
- (BOOL) weekIsEqual:(NSDate *)date and:(NSDate *)otherDate {
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *dateComponents = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit fromDate:date];
NSDateComponents *otherDateComponents = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit fromDate:otherDate];
return [dateComponents week] == [otherDateComponents week] && [dateComponents year] == [otherDateComponents year];
}
edit: In this line:
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
You don't pass NSWeekCalendarUnit
, therefore [dateComponent week]
returns NSUndefinedDateComponent
edit
Of course, it has to be NSWeekCalendarUnit
…
Starting from iOS8, this became much easier:
-(BOOL)isDate:(NSDate *)date1 onSameWeekAs:(NSDate *)date2
{
return [[NSCalendar currentCalendar] isDate:date1
equalToDate:date2
toUnitGranularity:NSCalendarUnitWeekOfYear];
}
From the documentation:
returns YES if both dates have equal date component for all units greater than or equal to the given unit, otherwise NO.
精彩评论