Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update:</strong> Note that this solution is specific to your case and assumes that store opening hours don't span two days. For example it won't work if the opening hour goes from Monday 9pm to Tuesday 10am. Since 10pm is after 9pm but not before 10am (within a day). So keep that in mind.</p> <p>I cooked up a function which will tell you if the time of one date is between two other dates (it ignores the year, month and day). There's also a second helper function which gives you a new NSDate with the year, month and day components "neutralized" (eg. set to some static value).</p> <p>The idea is to set the year, month and day components to be the same between all dates so that the comparison will only rely on the time.</p> <p>I'm not sure if it's the most efficient approach, but it works.</p> <pre><code>- (NSDate *)dateByNeutralizingDateComponentsOfDate:(NSDate *)originalDate { NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; // Get the components for this date NSDateComponents *components = [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate: originalDate]; // Set the year, month and day to some values (the values are arbitrary) [components setYear:2000]; [components setMonth:1]; [components setDay:1]; return [gregorian dateFromComponents:components]; } - (BOOL)isTimeOfDate:(NSDate *)targetDate betweenStartDate:(NSDate *)startDate andEndDate:(NSDate *)endDate { if (!targetDate || !startDate || !endDate) { return NO; } // Make sure all the dates have the same date component. NSDate *newStartDate = [self dateByNeutralizingDateComponentsOfDate:startDate]; NSDate *newEndDate = [self dateByNeutralizingDateComponentsOfDate:endDate]; NSDate *newTargetDate = [self dateByNeutralizingDateComponentsOfDate:targetDate]; // Compare the target with the start and end dates NSComparisonResult compareTargetToStart = [newTargetDate compare:newStartDate]; NSComparisonResult compareTargetToEnd = [newTargetDate compare:newEndDate]; return (compareTargetToStart == NSOrderedDescending &amp;&amp; compareTargetToEnd == NSOrderedAscending); } </code></pre> <p>I used this code to test it. You can see that the year, month and days are set to some random values and don't affect the time checking.</p> <pre><code>NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"yyyy:MM:dd HH:mm:ss"]; NSDate *openingDate = [dateFormatter dateFromString:@"2012:03:12 12:30:12"]; NSDate *closingDate = [dateFormatter dateFromString:@"1983:11:01 17:12:00"]; NSDate *targetDate = [dateFormatter dateFromString:@"2034:09:24 14:15:54"]; if ([self isTimeOfDate:targetDate betweenStartDate:openingDate andEndDate:closingDate]) { NSLog(@"TARGET IS INSIDE!"); }else { NSLog(@"TARGET IS NOT INSIDE!"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload