检查当前时间是否介于两个NSDate(iOS)之间

时间:2016-08-23 09:08:03

标签: ios objective-c nsdate

我正在制作一个简单的应用程序,用于检索附近餐馆的信息。我从外部Web服务获取所有数据。每个餐馆对象都有一个2016-08-21,其中包含该特定餐厅的工作时间。这个NSDictionary的结构如此......

NSDictionary

在我的应用程序中,我需要一个选项来查看餐厅现在是开放还是关闭。以及12小时格式的当天工作时间。

我对开发比较陌生,不知道如何处理这个问题。我可以简单地在循环中运行它并获取所有日期,但这似乎效率低下。

谢谢:)

2 个答案:

答案 0 :(得分:0)

您无法使用NSDate进行比较。你需要:

  1. 获取当前日期/时间(NSDate)并从午夜开始提取当前时间。
  2. 将字典中的时间从午夜转换为秒。它采用了一种奇怪的格式,例如,我假设1326将在下午1点半之后的26分钟内完成。## li>。
  3. 1:

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:(NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond)
                                          fromDate:[NSDate date]];
    NSInteger nowSeconds = ([comps hour] * 60 * 24) + ([comps minutes] * 60) + [comps seconds];
    

    2:

    + (NSInteger)dictTimeToSeconds:(NSInteger)dictTime
    {
        NSInteger hours = dictTime / 100;
        NSInteger minutes = dictTime % 100;
        return (hours * 60 * 24) + (minutes * 60);
    }
    
    ...
    
    NSArray *workingHours = thatDictionary[@"restaurant_working_hours"];
    for (NSDictionary *days in workingHours) {
        NSInteger openTime = [self dictTimeToSeconds:days[@"open"][@"time"]];
        NSInteger closeTime = [self dictTimeToSeconds:days[@"close"][@"time"]];
        if (openTime < nowSeconds && closeTime > nowSeconds) {
            NSLog(@"Open on day %@", days[@"open"][@"dayName"]);
        } else {
            NSLog(@"Closed on day %@", days[@"open"][@"dayName"]);
        }
    }
    

    注意:服务器数据的结构不必要地复杂。

答案 1 :(得分:0)

感谢@ Droppy ..这正是我在代码中所做的。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:[NSDate date]]

NSString *predicateString = [NSString stringWithFormat:@"close.dayName like '%@'",dayName];
NSPredicate *pred = [NSPredicate predicateWithFormat:predicateString];
NSArray *result = [restaurantWorkingHoursDict filteredArrayUsingPredicate:pred];

NSMutableDictionary *todayDict = [result firstObject];

NSString *closeString = [[todayDict objectForKey:@"close"] objectForKey:@"time"];
NSString *openString = [[todayDict objectForKey:@"open"] objectForKey:@"time"];

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"HHmm"];

NSDate *openDate = [outputFormatter dateFromString:openString];
NSDate *closeDate = [outputFormatter dateFromString:closeString];

[outputFormatter setDateFormat:@"h:mm a"];

NSString *openingHour = [outputFormatter stringFromDate:openDate];
NSString *closingHour = [outputFormatter stringFromDate:closeDate];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:(NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond)
                                      fromDate:[NSDate date]];
NSInteger nowSeconds = ([comps hour] * 60 * 60) + ([comps minute] * 60) + [comps second];

NSInteger closeSecond = [self dictTimeToSeconds:[closeString integerValue]];
NSInteger openSecond = [self dictTimeToSeconds:[openString integerValue]];

if (openSecond < nowSeconds && closeSecond > nowSeconds) {
    self.openLebal.text = [NSString stringWithFormat:@"Open Now from:%@ till:%@",openingHour,closingHour];
} else {
    self.openLebal.text = [NSString stringWithFormat:@"Closed Now. Open from:%@ till:%@",openingHour,closingHour];
}

....

- (NSInteger)dictTimeToSeconds:(NSInteger)dictTime{
    NSInteger hours = dictTime / 100;
    NSInteger minutes = dictTime % 100;
    return (hours * 60 * 60) + (minutes * 60);
}

做了一些改动。希望这会有助于其他人。