简体   繁体   English

Objective-C中的开放时间-两次NSDate之间的时间

[英]Open Hours in Objective-C - Time between two times NSDate

I am trying to perform a segue in Objective-C (XCode) for iOS devices, when the time right now is between two other fixed times. 我正在尝试在iOS设备的Objective-C(XCode)中执行搜索,此时的时间在其他两个固定时间之间。 Just like "Open Hours" for stores - when the time right now is between open and close hour. 就像商店的“营业时间”一样-当前时间在营业时间与营业时间之间。

Here is the code I have been working on - some of the code may look familiar, because I found some useful stuff on SO which helped me - but still I can't get it to work. 这是我一直在努力的代码-有些代码可能看起来很熟悉,因为我在SO上找到了一些有用的东西对我有所帮助-但我仍然无法使其正常工作。 It doesn't perform the segue when the time passes startTime. 时间过了startTime时,它不执行segue。 It should be in the specified time interval. 它应该在指定的时间间隔内。

The time is in 24-hour format. 时间为24小时格式。

 // set start time and end time
NSString *startTimeString = @"23:00";
NSString *endTimeString = @"05:00";

// set date formatter 24-hour format
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"HH:mm"];

// german timezone
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"de_DE"];


NSString *nowTimeString = [formatter stringFromDate:[NSDate date]];

// set NSDates for startTime, endTime and nowTime
NSDate *startTime   = [formatter dateFromString:startTimeString];
NSDate *endTime  = [formatter dateFromString:endTimeString];
NSDate *nowTime     = [formatter dateFromString:nowTimeString];

[formatter setLocale:locale];

// compare endTime and startTime with nowTime
NSComparisonResult result1 = [nowTime compare:endTime];
NSComparisonResult result2 = [nowTime compare:startTime];

if ((result1 == NSOrderedDescending) &&
    (result2 == NSOrderedAscending)){

    NSLog(@"Time is between");
    [self performSegueWithIdentifier:@"openHours" sender:self];

} else {
    NSLog(@"Time is not between");
}
  • thanks for taking your time to look at my question. 感谢您抽出宝贵时间来查看我的问题。 I have been searching and searching, trying and trying, but no luck in making it work yet. 我一直在搜索,搜索,尝试和尝试,但是使它正常工作还没有运气。 Hopefully your answers will help me. 希望您的回答对我有帮助。

You should little bit change you code 您应该稍微修改一下代码

// set NSDates for startTime, endTime and nowTime
        int startTime   = [self minutesSinceMidnight:[formatter dateFromString:startTimeString]];
        int endTime  = [self minutesSinceMidnight:[formatter dateFromString:endTimeString]];
        int nowTime     = [self minutesSinceMidnight:[formatter dateFromString:nowTimeString]];;

        [formatter setLocale:locale];


if (nowTime < endTime && nowTime > startTime) {
    NSLog(@"Time is between");
} else if (nowTime > endTime && nowTime < startTime) {
    NSLog(@"Time is between");
} else {
    NSLog(@"Time is not between");
}

And implement method for calculating time: 并实现计算时间的方法:

-(int) minutesSinceMidnight:(NSDate *)date
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned unitFlags =  NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags fromDate:date];

    return 60 * [components hour] + [components minute];
}

I am interested in an answer that works on ANY date, and not fixed dates for opening and closing. 我对适用于任何日期的答案都感兴趣,而不是针对打开和关闭的固定日期。

In that case the simplest approach would be using NSDateComponents. 在这种情况下,最简单的方法是使用NSDateComponents。 You could store hour, minutes and maybe weekday for opening and closing. 您可以存储小时,分钟,也可以存储工作日以进行打开和关闭。 to check for now you would break up [NSDate now] into the same NSDateComponents and cop are those. 现在检查,您可以将[NSDate now]分解为相同的NSDateComponents和cop。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM