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