Question

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.

Était-ce utile?

La solution

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!

Autres conseils

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
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top