检查当前时间是否在两个 NSDate 之间 (iOS)
Check if current time come in between two NSDate (iOS)
我正在制作一个简单的应用程序来检索有关附近餐馆的信息。我从外部网络服务获取所有数据。每个餐厅对象都有一个 NSDictionary
,其中包含该特定餐厅的工作时间。 NSDictionary
的结构如下..
"restaurant_working_hours" = (
{
close = {
day = 1;
dayName = Monday;
time = 0000;
};
open = {
day = 1;
dayName = Monday;
time = 1500;
};
},
{
close = {
day = 2;
dayName = Tuesday;
time = 0000;
};
open = {
day = 2;
dayName = Tuesday;
time = 1500;
};
},
{
close = {
day = 3;
dayName = Wednesday;
time = 0000;
};
open = {
day = 3;
dayName = Wednesday;
time = 1500;
};
},
{
close = {
day = 4;
dayName = Thursday;
time = 0000;
};
open = {
day = 4;
dayName = Thursday;
time = 1500;
};
},
{
close = {
day = 5;
dayName = Friday;
time = 0000;
};
open = {
day = 5;
dayName = Friday;
time = 1500;
};
}
);
在我的应用程序中,我需要一个选项来确定餐厅现在是开门还是关门。以及当天的工作时间,以 12 小时制表示。
我对开发还比较陌生,不知道如何处理这个问题。我可以简单地 运行 循环并获取所有日期,但这似乎效率低下。
谢谢 :)
您不能使用 NSDate
进行比较。您需要:
- 获取当前 date/time(如
NSDate
)并从午夜提取当前时间(以秒为单位)。
- 将字典中的时间转换为从午夜算起的秒数。它的格式很奇怪,我假设
1326
将是“下午 1 点后 26 分钟”。
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"]);
}
}
注意:服务器数据的结构不必要地复杂。
感谢@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);
}
做了一些修改。希望这会对其他人有所帮助。
我正在制作一个简单的应用程序来检索有关附近餐馆的信息。我从外部网络服务获取所有数据。每个餐厅对象都有一个 NSDictionary
,其中包含该特定餐厅的工作时间。 NSDictionary
的结构如下..
"restaurant_working_hours" = (
{
close = {
day = 1;
dayName = Monday;
time = 0000;
};
open = {
day = 1;
dayName = Monday;
time = 1500;
};
},
{
close = {
day = 2;
dayName = Tuesday;
time = 0000;
};
open = {
day = 2;
dayName = Tuesday;
time = 1500;
};
},
{
close = {
day = 3;
dayName = Wednesday;
time = 0000;
};
open = {
day = 3;
dayName = Wednesday;
time = 1500;
};
},
{
close = {
day = 4;
dayName = Thursday;
time = 0000;
};
open = {
day = 4;
dayName = Thursday;
time = 1500;
};
},
{
close = {
day = 5;
dayName = Friday;
time = 0000;
};
open = {
day = 5;
dayName = Friday;
time = 1500;
};
}
);
在我的应用程序中,我需要一个选项来确定餐厅现在是开门还是关门。以及当天的工作时间,以 12 小时制表示。
我对开发还比较陌生,不知道如何处理这个问题。我可以简单地 运行 循环并获取所有日期,但这似乎效率低下。
谢谢 :)
您不能使用 NSDate
进行比较。您需要:
- 获取当前 date/time(如
NSDate
)并从午夜提取当前时间(以秒为单位)。 - 将字典中的时间转换为从午夜算起的秒数。它的格式很奇怪,我假设
1326
将是“下午 1 点后 26 分钟”。
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"]);
}
}
注意:服务器数据的结构不必要地复杂。
感谢@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);
}
做了一些修改。希望这会对其他人有所帮助。