iOS开发 判定某个时间是否属于这个时间段
- (BOOL)isBetweenDate
{
//设置的是中国时间
NSString *startTime=@"13:01";
NSString *expireTime=@"20:01";
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// 时间格式,此处遇到过坑,建议时间HH大写,手机24小时进制和12小时禁止都可以完美格式化
[dateFormat setDateFormat:@"HH:mm"];
NSString * todayStr=[dateFormat stringFromDate:today];//将日期转换成字符串
// todayStr=@"12:01";
today=[ dateFormat dateFromString:todayStr];//转换成NSDate类型。日期置为方法默认日期
//startTime格式为 02:22 expireTime格式为 12:44
NSDate *start = [dateFormat dateFromString:startTime];
NSDate *expire = [dateFormat dateFromString:expireTime];
NSLog(@"today today ==%@",today);
NSLog(@"start start ==%@",start);
NSLog(@"expire expire ==%@",expire);
if ([today compare:start] == NSOrderedDescending && [today compare:expire] == NSOrderedAscending)
{
NSLog(@"属于------1");
return YES;
}
else
{
NSLog(@"不属于------2");
return NO;
}
return NO;
}