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

how to check current date time is in between given date times in iOS?

我必须检查我当前的日期时间是否在给定的 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 for checkVehicleRestriction .

这些是我使用的日期时间格式 02-06-2016 20:09:47 PM

我不确定您是如何计算开始日期和结束日期的,但是要简单地计算您当前的日期是否在两个日期之间,您可以这样做:

获取日期:

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(之后) = 当前日期在范围内

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

所以我们只需要检查两种情况

 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;
}