NSDate* firstTime = (NSDate *)openTime;//10:00AM
NSDate* secondTime = (NSDate *)closeTime;//10:00PM
NSDate* nowTime = (NSDate *)str;//3:09PM
NSComparisonResult result1 = [nowTime compare:firstTime];
NSComparisonResult result2 = [nowTime compare:secondTime];
if(result1==NSOrderedDescending && result2==NSOrderedAscending)
{
return TRUE;//------ expecting result is true as per sample
}
else
{
return FALSE;
}
我有这个代码,我期待结果是真的,我给了代码。但我得到的是假的。我的目的是检查我在开始时间和结束时间之间的当前时间。请帮帮我。
答案 0 :(得分:4)
尝试
NSString *fromDateString = @"10:00AM";
NSString *toDateString = @"10:00PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"hh:mma"];
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
NSString *nowDateString = [dateFormatter stringFromDate:[NSDate date]];
NSDate *firstTime = [dateFormatter dateFromString:fromDateString];
NSDate *secondTime = [dateFormatter dateFromString:toDateString];
NSDate *nowTime = [dateFormatter dateFromString:nowDateString];
NSComparisonResult result1 = [nowTime compare:firstTime];
NSComparisonResult result2 = [nowTime compare:secondTime];
if ((result1 == NSOrderedDescending) &&
(result2 == NSOrderedAscending)){
return TRUE;
}else{
return FALSE;
}
答案 1 :(得分:0)
我尝试在OSX中使用一些虚拟数据,您需要使用原始值进行更改
NSDate *nowTime=[NSDate date];
NSDate *firstTime=[NSDate dateWithNaturalLanguageString:@"22 April 2013"];
NSDate *secondTime=[NSDate dateWithNaturalLanguageString:@"23 April 2013"];
BOOL first=([nowTime compare:firstTime] == NSOrderedDescending);//now time is more then firsttime
BOOL second=([nowTime compare:secondTime] == NSOrderedAscending);//now time is more then secondtime
if(first && second){
;//return YES;
NSLog(@"Between");
}
else{
;//return NO;
NSLog(@"Not in Between");
}
编辑:
我把时间检查为:
NSDate *firstTime=[NSDate dateWithNaturalLanguageString:@"5PM"];
NSDate *secondTime=[NSDate dateWithNaturalLanguageString:@"10PM"];
它运作良好。
答案 2 :(得分:0)
检查出来
NSString *openTime = @"10:00 AM";
NSString *closeTime = @"10:00 PM";
NSString *str = @"11:09 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mma"];
NSDate *firstTime = [[NSDate alloc]init];
firstTime = [dateFormatter dateFromString:openTime];
NSDate* secondTime = [[NSDate alloc]init];
secondTime = [dateFormatter dateFromString:closeTime];
NSDate* nowTime = [[NSDate alloc]init];
nowTime = [dateFormatter dateFromString:str];
NSTimeInterval interval =[[NSDate date] timeIntervalSince1970];
NSLog(@"interval : %f",interval);
if([nowTime timeIntervalSince1970] >= [firstTime timeIntervalSince1970] && [nowTime timeIntervalSince1970] <= [secondTime timeIntervalSince1970])
{
return YES;// Between Interval
}
else
{
return NO; // Not between Interval
}
希望它对你有所帮助。
答案 3 :(得分:0)
将您的日期转换为timeInterval,例如:
NSDate* firstTime = (NSDate *)openTime;//10:00AM
NSDate* secondTime = (NSDate *)closeTime;//10:00PM
NSDate* nowTime = (NSDate *)str;//3:09PM
NSTimeInterval firstTimeInterVal = [firstTime NSTimeIntervalSince1970]
NSTimeInterval secondTimeInterVal = [secondTime NSTimeIntervalSince1970]
NSTimeInterval nowTimeInterval = [nowTime NSTimeIntervalSince1970];
然后比较:
if (nowTimeInterval > firstTimeInterVal && nowTimeInterval < secondTimeInterVal) {
/**Between**/
}else{
/**Not Between**/
}