-1
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;
}

我有这个代码,我期待结果是我在代码中给出的。但我得到的是错误的。我的目标是在开始时间和结束时间之间检查我的当前时间。请帮我。

4

4 回答 4

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;
}
于 2013-04-29T10:34:59.847 回答
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**/
}
于 2013-04-29T11:26:38.723 回答
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"];

它运行良好。

于 2013-04-29T09:51:57.127 回答
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
    }

希望它可以帮助你。

于 2013-04-29T10:08:36.400 回答