-2

I have to check my current date time is in between the given 2 set of date time (need to check both time and date part of NSDate).

Here is my code

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

When I run this code i am always getting YES for checkVehicleRestriction .

These are the date time format am using 02-06-2016 20:09:47 PM

AnthonyR
  • 3,485
  • 1
  • 18
  • 41
Bangalore
  • 1,572
  • 4
  • 20
  • 50

1 Answers1

1

I'm not sure how you are calculating your startDate and end Date, but to simply calculate if your current date is in between two dates or not, you can do this:

Get the dates:

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

and to check if current date is in between two dates

Date1(before current) CurrentDate Date2(after current) = CurrentDate is in Range

Date2(before current) CurrentDate Date1(after current) = CurrentDate is in Range

So we need to check for only two cases

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

In these both cases the current date is said to be in range .

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

TO simplify this method you can just do:

 if ((([date compare:firstDate]==NSOrderedDescending) && ([date compare:lastDate]==NSOrderedAscending)) || (([date compare:firstDate]==NSOrderedAscending) && ([date compare:lastDate]==NSOrderedDescending))) {
    NSLog(@"date is in range");
    return YES;
}
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • 1
    Maybe too tricky, but `[date compare:firstDate] != [date compare:lastDate]` would be enough to do the job. (With a different (better?) behavior for being an equal date. But you can change that with +.) – Amin Negm-Awad Jun 02 '16 at 15:34
  • 2
    I'm not into this much, but i'm glad you pointed me to a much more easier way of doing it. For starters it would be hard to understand one single line of code that does two things. But thanks for coming up with that @AminNegm-Awad – Teja Nandamuri Jun 02 '16 at 15:37
  • 1
    This is, why I said: "Maybe too tricky". I agree. – Amin Negm-Awad Jun 02 '16 at 15:38
  • working fine ,thanks – Bangalore Jun 02 '16 at 18:48