Reputation: 585
Good afternoon,
How do you check if a date is between two dates? I know I have to convert the data strings to NSDate values first:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *date1 = [dateFormatter dateFromString:@"01/01/2001"];
NSDate *date2 = [dateFormatter dateFromString:@"01/01/2010"];
NSDate *userDate = [dateFormatter dateFromString:@"12/12/2001"];
And then I have to use an if-statement, but I am not quite sure how to go about it. Here is what I need:
if (userDate is between date1 and date2) {
}
Any help with the if-statement would be appreciated. Thank you!
Upvotes: 1
Views: 2034
Reputation: 1536
of the top of my head so check the syntax ;-)
if (([userDate laterDate: date1] == userDate) && ([userDate laterDate: date2] == date2)){
}
Upvotes: 2
Reputation: 11841
if(([userDate compare: date1] == NSOrderedDescending) && ([userDate compare: date2] == NSOrderedAscending)){
//do something
}
Upvotes: 1