- (BOOL)checkDateRange {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:YOUR_DATE_IN_TEXTFIELD];
//Hundred year ago date from today
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDate *now = [NSDate date];
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:now];
[comps setYear:[comps year] - 100];
NSDate *hundredYearsAgo = [gregorian dateFromComponents:comps];
Comparison of two dates
NSComparisonResult result, result2;
result = [date compare:now]; // comparing two dates
result2 = [date compare:hundredYearsAgo]; // comparing two dates
if(result==NSOrderedAscending && result 2==NSOrderedDescending)
{
NSLog(@"Between the StartDate and Enddate");
return YES;
}
else
{
NSLog(@"date in not in rang e...");
UIAlertView * al = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Please enter valid date" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[al show];
return NO;
}
}