下午好,
如何检查日期是否在两个日期之间?我知道我必须首先将数据字符串转换为NSDate值:
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"];
然后我必须使用if语句,但我不太清楚如何去做。这就是我需要的:
if(userDate介于date1和date2之间) {
}
对if语句的任何帮助都将不胜感激。谢谢!
答案 0 :(得分:4)
使用NSDate -compare:
:
if (([date1 compare:userDate] == NSOrderedAscending) && ([date2 compare:userDate] == NSOrderedDescending)) {
// Do something
}
答案 1 :(得分:2)
所以检查语法; - )
if (([userDate laterDate: date1] == userDate) && ([userDate laterDate: date2] == date2)){
}
答案 2 :(得分:1)
if(([userDate compare: date1] == NSOrderedDescending) && ([userDate compare: date2] == NSOrderedAscending)){
//do something
}