NSDate *startDate = …;
NSDate *endDate = …; // make sure endDate is later than startDate
NStimeInterVal endInterval;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&startDate interval:NULL forDate:startDate];
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&endDate interval:&endInterval forDate:endDate];
endDate = [endDate dateByAddingTimeInterval:endInterval-0.001];
This will create a start date for time 0:00 and an endate of 23:59:59.999 in the default timezone
Now use the compare method
if ([givenDate compare:startDate] == NSOrderedAscending){
//date is before both dates
} else if ([givenDate compare:endDate] == NSOrderedDescending){
// date lies after both dates
} else {
//givenDate lies between both dates.
}
it became apparent, that you want to check it for any year. Just set the yer to a year - like 1 - for the comparison for all involved dates.
NSDateComponents *comps = [aCalendar components:(NSUIntegerMax - NSYearCalendarUnit ) fromDate:date];
comps.year = 1;
NSDate *year1Date = [aCalendar dateFromComponents:comps];
NSDate *startDate = …;
NSDate *endDate = …; // make sure endDate is later than startDate
NStimeInterVal endInterval;
NSDateComponents *comps = [aCalendar components:(NSUIntegerMax - NSYearCalendarUnit ) fromDate:startDate];
comps.year = 1;
NSDate *year1StartDate = [aCalendar dateFromComponents:comps];
comps = [aCalendar components:(NSUIntegerMax - NSYearCalendarUnit ) fromDate:endDate];
comps.year = 1;
NSDate *year1EndDate = [aCalendar dateFromComponents:comps];
comps = [aCalendar components:(NSUIntegerMax - NSYearCalendarUnit ) fromDate:givenDate];
comps.year = 1;
NSDate *year1givenDate = [aCalendar dateFromComponents:comps];
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&year1StartDate interval:NULL forDate:year1StartDate;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&endDate interval:&endInterval forDate:endDate];
endDate = [endDate dateByAddingTimeInterval:endInterval-0.001];
if ([year1givenDate compare:year1StartDate] == NSOrderedAscending){
//date is before both dates
} else if ([year1givenDate compare:year1EndDate] == NSOrderedDescending){
// date lies after both dates
} else {
//givenDate lies between both dates.
}