I know how to compare dates.In my case, let Date1 = 2011-10-14 & Date2 =2011-10-20 .And I want to check if 15th Oct lie between Date1 and Date2.I'm using following codes for this:
if ( [date compare:Date1] == NSOrderedDescending && [date compare:Date2] == NSOrderedAscending) {
//write codes
}
But this will not check if 15th oct lie between 14th and 20th Oct.rather it check for whole date. So, How will I implement this. Thnx in advance.
I hope the following code would help you. Assuming Date1 and Date2 you have,
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger desiredComponents = (NSDayCalendarUnit | NSMonthCalendarUnit);
NSDateComponents *firstComponents = [calendar components:desiredComponents fromDate:Date1];
NSDateComponents *secondComponents = [calendar components:desiredComponents fromDate:Date2];
NSDate *firstWOYear = [calendar dateFromComponents:firstComponents];
NSDate *SecondWOYear = [calendar dateFromComponents:secondComponents];
NSComparisonResult result = [firstWOYear compare:SecondWOYear];
if (result == NSOrderedAscending) {
//Date1 is before Date2
} else if (result == NSOrderedDescending) {
//Date1 is after Date2
} else {
//Date1 is the same day/month as Date2
}