-1

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.

marciokoko
  • 4,988
  • 8
  • 51
  • 91

2 Answers2

1

Use the NSDate compare selector:

if ([date1 compare:date2]==NSOrderedDescending) {
   // date1 is after date2
}

There are also earlierDate, timeIntervalSinceDate, etc.

UPDATE: For example testing now is between 2 dates:

    NSDate *now = [NSDate date];
    if ([date1 compare:now]==NSOrderedAscending && [date2 compare:now]==NSOrderedDescending) {
        // now is between the 2 dates
    }
Janene Pappas
  • 1,366
  • 12
  • 26
  • Thx but I need to know if now falls between date1 and date2 to determine whether the store is open or closed. – marciokoko Jul 26 '13 at 01:34
  • I added another example to test if now is between the 2 dates. HTH – Janene Pappas Jul 26 '13 at 16:23
  • Thx but its still not working, I added the logs of the dates im comparing. – marciokoko Jul 27 '13 at 03:51
  • You've set timezones for the dates you parsed but not for "now". Make sure the dates you are comparing are all in the same zone. Please add the exact output - what you appended doesn't match the code log statements, so it's hard to debug. – Janene Pappas Jul 28 '13 at 19:16
1

NSDateFormatter defaults to the year 2000 when no year is supplied. If you want your openDate and closeDate to be in the year 0001 instead of 2000, try this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Add year to expected date format
[dateFormatter setDateFormat:@"yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];

NSString *openDateString = (NSString*)[timeStringsArray2 objectAtIndex:0];
NSString *closeDateString = (NSString*)[timeStringsArray2 objectAtIndex:1];

//make new strings with year 0001 + original time
NSString *adjustedOpenDateString = [NSString stringWithFormat:@"%@ %@", @"0001", openDateString];
NSString *adjustedCloseDateString = [NSString stringWithFormat:@"%@ %@", @"0001", closeDateString];

NSDate *openDate = [dateFormatter dateFromString:adjustedOpenDateString];
NSDate *closeDate = [dateFormatter dateFromString:adjustedCloseDateString];

This should set the date formatter to look for a year, and add the year 0001 to the strings you are creating your dates from. Unfortunately I am away from Xcode at the moment so I cannot guarantee there are no typos!

Jake Spencer
  • 1,117
  • 7
  • 13
  • Thanks, I got it working as far as adding 2013 as the year. But since Im still using 01-01 as the default day and month, Im assuming thats why the test still fails. I would have to get the current day and month in order to be able to add them as well and then make the compare. Im thinking thats done via NSDateComponents, right? – marciokoko Jul 26 '13 at 02:42
  • Here is a link that shows how to get an NSDate for today at midnight, it may be helpful: http://stackoverflow.com/questions/9040319/how-can-i-get-an-nsdate-object-for-today-at-midnight – Jake Spencer Jul 26 '13 at 03:07
  • You can use NSDateFormatter to create the current date in char form, sans time, then concat with the char time and run back through NSDateFormatter (with a different format). But at some point it makes more sense to just decode the hours and minutes values in the start/end times and compare those, vs converting them to NSDates. – Hot Licks Jul 27 '13 at 04:30