【问题标题】:How to compare time between a time interval in iphone [duplicate]如何比较iphone中时间间隔之间的时间[重复]
【发布时间】:2013-04-22 22:32:27
【问题描述】:
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;
}

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

【问题讨论】:

  • “openTime”和“closeTime”的“日期”组件是什么。尝试一些“NSLog”。
  • 两个“重复”链接都链接到日期和时间比较问题 - OP(似乎如此)只想比较 NSDate 的时间分量
  • @SubinKurian:你如何真正(在你的代码中)实例化openTimecloseTime
  • 没有我们拥有的日期组件.. 只有时间,它不会以字符串形式出现在 10:00AM-10:00PM 的任何时间,我将它解析为两个组件,我需要比较与当前时间。当前时间是否介于此时间之间

标签: iphone ios ios4


【解决方案1】:

试试

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

【讨论】:

  • 谢谢 我想知道如果我们在 2 天内露营会有什么结果。意思是比较 2 天内的时间,例如下午 4 点和凌晨 4 点
【解决方案2】:

我在 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"];

而且效果很好。

【讨论】:

  • 我们试过了,第一部分还可以(第一部分)第二部分没有,这是错误的。我们会怎么做?
  • 检查时间是否真的在范围内...
  • 是的.. 现在时间是下午 3:37,我正在与上午 10:00 和晚上 10:00 进行比较
  • @SubinKurian:: 检查我编辑的答案
  • 你能检查一下吗?你可以使用 10:00AM 和 10:00PM 作为 2013 年 4 月 22 日和 2013 年 4 月 23 日的时间。我认为在日期情况下是正确的,但随着时间的推移,它不会变成真的
【解决方案3】:

看看这个

  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
    }

希望对你有帮助。

【讨论】:

  • 它无论如何都不起作用。这是错误的
  • @Subin 我已经更新了我的答案。检查一下
【解决方案4】:

将您的日期转换为 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**/
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    相关资源
    最近更新 更多