【问题标题】:How to check if a date is into a range of dates with a specific repetition?如何检查日期是否在具有特定重复的日期范围内?
【发布时间】:2012-05-19 14:25:02
【问题描述】:

我想创建一个简单的药丸提醒 iOS 应用程序(让我们暂时将通知放在一边)。 我想为具有重复的药丸创建单个数据库记录,并检查特定日期是否与重复“生成”的日期相交。

示例: 我设定了一个从 4 月 12 日开始到 4 月 20 日结束的服药期,每 2 天重复一次,下午 3 点。 所以这个药片在这个日期有效:

  • 4 月 12 日下午 3 点
  • 4 月 14 日下午 3 点
  • 4 月 16 日下午 3 点
  • 4 月 18 日下午 3 点
  • 4 月 20 日下午 3 点

问题:

  • 哪种类型的数据可以描述“每 2 天”的信息? NSDateInterval 会是一个很好的解决方案吗?

  • 如何验证特定日期是否符合我的重复计划? (即检查 4 月 13 日是否是上一个示例的有效日期并得到“否”作为答案)

【问题讨论】:

    标签: objective-c ios date


    【解决方案1】:

    这是一个应该满足您的条件的示例类:

    main.m

    #import <Foundation/Foundation.h>
    #import "DateChecker.h"
    
    int main(int argc, const char * argv[])
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        // Specify a period in days
        NSInteger period = 2;
    
        // Set the start/end/current days.  These can be retrieved from a database or elsewhere
        NSDate *startDate = [NSDate dateWithString:@"2012-04-12 00:00:00 +0000"];
        NSDate *endDate = [NSDate dateWithString:@"2012-04-20 00:00:00 +0000"];
        NSDate *currentDate = [NSDate dateWithString:@"2012-04-14 00:00:00 +0000"];
    
        // Actually do the checking.  This could alternatively be offloaded to a function in DateChecker
        if([DateChecker date:currentDate isBetweenDate:startDate andDate:endDate])
        {
            if([DateChecker date:currentDate fallsOnStartDate:startDate withInterval:period])
            {
                NSLog(@"The date %@ falls in the specified date period & is on the interval date.", currentDate);
            }
            else
            {
                NSLog(@"The date %@ falls in the specified date period & is NOT on the interval date.", currentDate);
            }
        }
        else
        {
            NSLog(@"The date %@ does not fall in the specified date period.", currentDate);
        }
    
        [pool release];
    
        return 0;
    }
    

    日期检查器.h

    #import <Foundation/Foundation.h>
    
    @interface DateChecker : NSObject
    {
    }
    + (BOOL)date:(NSDate*)date fallsOnStartDate:(NSDate*)beginDate withInterval:(NSInteger)daysInterval;
    + (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate;
    
    @end
    

    日期检查器.m

    #import "DateChecker.h"
    
    @implementation DateChecker
    
    + (BOOL)date:(NSDate*)date fallsOnStartDate:(NSDate*)beginDate withInterval:(NSInteger)daysInterval
    {
        NSDateComponents *components;
        NSInteger days;
    
        // Get the number of days since the start date
        components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit fromDate: beginDate toDate: date options: 0];
        days = [components day];
    
        // If the number of days passed falls on the interval, then retrun YES
        if(days % daysInterval == 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    
    + (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
    {
        if ([date compare:beginDate] == NSOrderedAscending)
            return NO;
    
        if ([date compare:endDate] == NSOrderedDescending) 
            return NO;
    
        return YES;
    }
    
    @end
    

    这应该让您了解如何编写一个可以处理您的要求的类。我将它作为命令行应用程序运行,它似乎工作正常。函数

    + (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
    

    是从How to Check if an NSDate occurs between two other NSDates慷慨地获得的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 2015-02-13
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多