首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查当前时间是否在ios中指定的范围内?

问如何检查当前时间是否在ios中指定的范围内?
EN

Stack Overflow用户
提问于 2012-10-27 16:41:02
回答 1查看 4.6K关注 0票数 4

可能重复: Determine if current local time is between two times (ignoring the date portion)

在iOS中,如何执行以下操作:

我有两个NSDate对象,它们表示商店的打开和关闭时间。这些对象中的时间是准确的,但日期未指定(无论日期如何,存储同时打开和关闭)。如何检查当前时间是否介于此时间范围内?

注意,如果它能帮助打开和结束时间采用NSDate对象以外的另一种格式,我对此没有意见。目前,我只是从文件中读取日期字符串(如"12:30“),并使用日期格式化程序创建匹配的NSDate对象。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-27 19:13:47

更新:注意到此解决方案是特定于您的情况,并假设商店的营业时间不超过两天。例如,如果开放时间从星期一晚上9点到星期二上午10点,它就无法工作。因为晚上10点是晚上9点以后,但不是在上午10点之前(一天之内)。所以记住这一点。

我编写了一个函数,它将告诉您一个日期的时间是否介于另外两个日期之间(它忽略了年、月和日)。还有第二个帮助函数,它为您提供了一个新的NSDate,其中包含“中和”的年、月和日组件(例如。设置为某些静态值)。

这样做的目的是将年、月和日组件设置为所有日期之间的相同,这样比较将只依赖于时间。

我不确定这是否是最有效的方法,但有效。

代码语言:javascript
复制
- (NSDate *)dateByNeutralizingDateComponentsOfDate:(NSDate *)originalDate {
    NSCalendar *gregorian = [[[NSCalendar alloc]
                              initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

    // Get the components for this date
    NSDateComponents *components = [gregorian components:  (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate: originalDate];

    // Set the year, month and day to some values (the values are arbitrary)
    [components setYear:2000];
    [components setMonth:1];
    [components setDay:1];

    return [gregorian dateFromComponents:components];
}

- (BOOL)isTimeOfDate:(NSDate *)targetDate betweenStartDate:(NSDate *)startDate andEndDate:(NSDate *)endDate {
    if (!targetDate || !startDate || !endDate) {
        return NO;
    }

    // Make sure all the dates have the same date component.
    NSDate *newStartDate = [self dateByNeutralizingDateComponentsOfDate:startDate];
    NSDate *newEndDate = [self dateByNeutralizingDateComponentsOfDate:endDate];
    NSDate *newTargetDate = [self dateByNeutralizingDateComponentsOfDate:targetDate];

    // Compare the target with the start and end dates
    NSComparisonResult compareTargetToStart = [newTargetDate compare:newStartDate];
    NSComparisonResult compareTargetToEnd = [newTargetDate compare:newEndDate];

    return (compareTargetToStart == NSOrderedDescending && compareTargetToEnd == NSOrderedAscending);
}

我用这段代码来测试它。您可以看到,年、月和日被设置为一些随机值,不影响时间检查。

代码语言:javascript
复制
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy:MM:dd HH:mm:ss"];

NSDate *openingDate = [dateFormatter dateFromString:@"2012:03:12 12:30:12"];
NSDate *closingDate = [dateFormatter dateFromString:@"1983:11:01 17:12:00"];
NSDate *targetDate = [dateFormatter dateFromString:@"2034:09:24 14:15:54"];

if ([self isTimeOfDate:targetDate betweenStartDate:openingDate andEndDate:closingDate]) {
    NSLog(@"TARGET IS INSIDE!");
}else {
    NSLog(@"TARGET IS NOT INSIDE!");
}
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13102082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档