I get 7:00AM and 10:00PM as NSStrings from a webservice. I convert them to NSDate using this code block:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];
NSString *openDateString = (NSString*)[timeStringsArray2 objectAtIndex:0];
NSString *closeDateString = (NSString*)[timeStringsArray2 objectAtIndex:1];
NSDate *openDate = [dateFormatter dateFromString:openDateString];
NSDate *closeDate = [dateFormatter dateFromString:closeDateString];
if ([self timeCompare:openDate until:closeDate]) {
NSLog(@"OPEN-timeCompare");
} else {
NSLog(@"CLOSED-timeCompare");
}
This is the compare method:
-(BOOL)timeCompare:(NSDate*)date1 until:(NSDate*)date2{
NSDate *date = [NSDate date];
NSLog(@"open:%@ now:%@ close:%@", date1, date, date2);
return ([date1 compare:date] == NSOrderedAscending && [date2 compare:date] == NSOrderedDescending);
}
So when I compare these values, I am comparing:
open:2013-07-26 12:00:00 +0000
now:2013-07-27 03:50:30 +0000 close:2013-07-27 03:00:00 +0000 CLOSED-timeCompare
Im not sure why because right now its actually 950pm which is 10 minutes before 10:00pm. It shouldn't be equivalent to a time past close time even if it IS in UTC.