如何检查当前日期时间是否在iOS中的给定日期时间之间?

时间:2016-06-02 14:43:01

标签: ios objective-c nsdate

我必须检查我当前的日期时间是否在给定的2组日期时间之间(需要检查NSDate的时间和日期部分。)

这是我的代码

    NSString *startingDate = [[[vehicle timeRestrictions] objectAtIndex:i] valueForKey:@"start_ts"];
    NSString *endingDate = [[[vehicle timeRestrictions] objectAtIndex:i] valueForKey:@"end_ts"];
    NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:[startingDate longLongValue]/1000];
    NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:[endingDate longLongValue]/1000];
    NSLog(@"Start Date %@",startDate);


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
    [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss a"];  // here replace your format dd.MM.yyyy
    NSLog(@"result: %@", [dateFormatter stringFromDate:[NSDate date]]);


    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    // [dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ss  ssZZZZZ"];
    [dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:ss "];

    BOOL checkVehicleRestriction = [self getDatePartForRestriction:[dateFormat stringFromDate:startDate] :[dateFormat stringFromDate:endDate]];
-(BOOL)getDatePartForRestriction:(NSString *)date :(NSString *)endDate{

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"];
    [dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    NSLog(@"%@ - %@",date,endDate);
    if([self isDate:[NSDate date] inRangeFirstDate:[dateFormat dateFromString:date] lastDate:[dateFormat dateFromString:endDate]]){
        return YES;
    }

    return NO;

}



- (BOOL)isDate:(NSDate *)date inRangeFirstDate:(NSDate *)firstDate lastDate:(NSDate *)lastDate {
    return !([date compare:firstDate] == NSOrderedAscending) && !([date compare:lastDate] == NSOrderedDescending);
}

当我运行此代码时,我YES总是获得checkVehicleRestriction

这些是使用02-06-2016 20:09:47 PM

的日期时间格式

1 个答案:

答案 0 :(得分:1)

我不确定你是如何计算你的startDate和结束日期的,但是为了简单计算你当前的日期是否在两个日期之间,你可以这样做:

获取日期:

NSString *startingDate = @"01-06-2016 20:09:47 PM";
NSString *endingDate = @"08-06-2016 20:09:47 PM";

NSDateFormatter *startForm=[[NSDateFormatter alloc]init];
[startForm setDateFormat:@"dd-MM-yyyy HH:mm:ss a"];
NSDate *startDate = [startForm dateFromString:startingDate];
NSDate *endDate = [startForm dateFromString:endingDate];

并检查当前日期是否在两个日期之间

Date1在当前之前)CurrentDate Date2在当前之后)= CurrentDate在范围内

Date2在当前之前)CurrentDate Date1在当前之后)= CurrentDate在范围内

所以我们只需检查两个案例

 case A - (startDateIsOld && !endDateIsOld)
 case B-  (!startDateIsOld && endDateIsOld)

在这两种情况下,当前日期都在范围内。

- (BOOL)isDate:(NSDate *)date inRangeFirstDate:(NSDate *)firstDate lastDate:(NSDate *)lastDate {

    BOOL startDateIsOld = false;
    BOOL endDateIsOld = false;

    if ([date compare:firstDate]==NSOrderedAscending) {
    NSLog(@"first date is after current date");
    startDateIsOld=NO;
    }
    if ([date compare:lastDate]==NSOrderedDescending){
    NSLog(@"last date is before current date");
    endDateIsOld=YES;
    }
    if ([date compare:firstDate]==NSOrderedDescending) {
    NSLog(@"first date is before current date");
    startDateIsOld=YES;
    }
    if ([date compare:lastDate]==NSOrderedAscending){
    NSLog(@"last date is after current date");
    endDateIsOld=NO;
    }
    if ((startDateIsOld && !endDateIsOld) || (!startDateIsOld && endDateIsOld) ) {
     NSLog(@"date is in range");
    return YES;
   }
    return NO;
}

要简化此方法,您可以这样做:

 if ((([date compare:firstDate]==NSOrderedDescending) && ([date compare:lastDate]==NSOrderedAscending)) || (([date compare:firstDate]==NSOrderedAscending) && ([date compare:lastDate]==NSOrderedDescending))) {
    NSLog(@"date is in range");
    return YES;
}