0

Comparing dates is quite complex. I have an app that is comparing opening and closing dates for stores and it works great for times in the same day, i.e. opening a 8am and closing at 5pm the same day.

Here is the code that compares the time:

if ([self timeCompare:openDate until:closeDate withNow:now]) {
        NSLog(@"TIMECOMPARATOR = timeCompare>OPEN");
        status = YES;
    } else {
        NSLog(@"TIMECOMPARATOR = timeCompare>CLOSED");
        status = NO;
    }

    return status;

This calls the following method:

+(BOOL)timeCompare:(NSDate*)date1 until:(NSDate*)date2 withNow:(NSDate*)now{
    NSLog(@"TIMECOMPARE = open:%@ now:%@ close:%@", date1, now, date2);
    return ([date1 compare:now] == NSOrderedAscending && [date2 compare:now] == NSOrderedDescending);
}

The problem comes when the closing time is "assumed" by a person but of course not by a computer, to close at the next day, such as 7am to 2am. I obviously mean the next day. How do I accommodate for this to signal the computer to be the next day?

marciokoko
  • 4,988
  • 8
  • 51
  • 91
  • If the closing time is earlier than the opening time then it must be the next day. – Hot Licks Dec 27 '13 at 16:59
  • Perhaps you can use some ideas from http://stackoverflow.com/questions/20440333/compare-current-time-and-compare-it-with-two-nsstring-time-in-objective-c/20441330#20441330. – Martin R Dec 27 '13 at 17:25
  • Yes Hotlicks but how do I tell my compiler that? :) – marciokoko Dec 27 '13 at 17:32
  • Thx MartinR, I tried it doesn't seem to work for 7am to 10pm which should be open but is closed. 8-630pm works, 7-2am works, but 7-1030pm doesn't, 8am-11pm doesn't work...why? – marciokoko Dec 27 '13 at 18:09
  • @marciokoko: That answer was written for a 24 hour input format, not for 12 hour AM/PM input. How exactly are your opening/closing times given? As a string or as NSDate? – Martin R Dec 27 '13 at 18:28
  • As a string which is 7:00 AM - 10:00 PM. So that i have 12hr format. – marciokoko Dec 27 '13 at 19:02
  • @marciokoko: I have modified the code for 12hr format, see below. – Martin R Dec 27 '13 at 20:01
  • Writing a routine to convert the 12 hour format to a number is relatively straight-forward, or you can use an NSDateFormatter to do it. – Hot Licks Dec 28 '13 at 06:00

4 Answers4

2

Compare the date's unix time. It will be accurate regardless of date as it is constantly increasing.

coneybeare
  • 33,113
  • 21
  • 131
  • 183
  • 1
    Yes, something like [date timeIntervalSince1970] makes everything easy. – 0xFADE Dec 27 '13 at 16:58
  • Ok how would I implement that given what I have? – marciokoko Dec 27 '13 at 17:34
  • The ultimate question is which closing time is later, store 1 or store 2, so you simply take the numeric values of [date1 timeIntervalSince1970] and [date2 timeIntervalSince1970], then see which one is larger. – coneybeare Dec 27 '13 at 17:35
  • Note that is does not make any difference if you compare `[date1 compare:date2]` or if you compare the corresponding `timeIntervalSince1970` values. – Martin R Dec 27 '13 at 20:00
  • Well, what i need to determine is if "now" is between open and close time, but taking into account that closing time is "am" then it means the next day, not the same day. – marciokoko Dec 27 '13 at 20:00
  • So then you would need to see if the [[NSDate date] timeIntervalSince1970] is in between the values from the date1 and date 2 ie 1388178151 > now > 1388189264 for example – coneybeare Dec 27 '13 at 21:18
1

First you have to convert the strings "7:00 AM", "10:00 PM" to a NSDate from the current day. This can be done e.g. with the following method:

- (NSDate *)todaysDateFromAMPMString:(NSString *)time
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

    // Get year-month-day for today:
    [fmt setDateFormat:@"yyyy-MM-dd "];
    NSString *todayString = [fmt stringFromDate:[NSDate date]];

    // Append the given time:
    NSString *todaysTime = [todayString stringByAppendingString:time];

    // Convert date+time string back to NSDate:
    [fmt setDateFormat:@"yyyy-MM-dd h:mma"];
    NSDate *date = [fmt dateFromString:todaysTime];
    return date;
}

Then you can proceed as in https://stackoverflow.com/a/20441330/1187415:

// Some example values:
NSString *strOpenTime = @"10:00 PM"; 
NSString *strCloseTime = @"2:00 AM";

NSDate *openTime = [self todaysDateFromAMPMString:strOpenTime];
NSDate *closeTime = [self todaysDateFromAMPMString:strCloseTime];

if ([closeTime compare:openTime] != NSOrderedDescending) {
    // closeTime is less than or equal to openTime, so add one day:
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comp = [[NSDateComponents alloc] init];
    [comp setDay:1];
    closeTime = [cal dateByAddingComponents:comp toDate:closeTime options:0];
}

NSDate *now = [NSDate date];
if ([now compare:openTime] != NSOrderedAscending &&
    [now compare:closeTime] != NSOrderedDescending) {
    // Shop is OPEN
} else {
    // Shop is CLOSED
}
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

This is an app design question, not a coding question. You need to define a clear vocabulary for the user to communicate what they mean. I would suggest adding a UISwitch to the screen, with 2 positions on it: "Today" and "Tomorrow". You can add some program logic that takes a guess as to times where you think the user is talking about a date tomorrow, and set the default switch to the "tomorrow" state in that case, but the user should be able to tell you what they mean.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • No, this has nothing to do with the user. The app is reading this off of a web service. The web service simply returns opening and closing time for a particular store. It returns 8:00am - 5:00pm. But it can also return 8:00am - 2:00am. – marciokoko Dec 27 '13 at 17:32
0

How about you just add a check whether all the three NSDate are having the same date (DDMMYYYY) value?

Akshat Singhal
  • 1,801
  • 19
  • 20