I hope you can help me with this:
I´m writing a App that should show if a shop has actually open.
For this i declared for each day and each opening times different variables / NSDate-Objects like (setting the dateformatter I´ve done before):
NSDate *mondayMorningOpening = [dateFormatter dateFromString:@"08:00"];
NSDate *montagMorningClosing = [dateFormatter dateFromString:@"12:30"];
NSDate *mondayNoonOpening = [dateFormatter dateFromString:@"15:00"];
NSDate *montagNoonClosing = [dateFormatter dateFromString:@"21:30"];
I´ve also set a NSDate for the actual time:
NSString *tempDate1 = [dateFormatter stringFromDate:date];
NSDate *actualTime = [dateFormatter dateFromString:tempDate1];
Now I want to calculate if the actual time is before or after the time range for the opening times of the shop. I´ve done this:
if ([actualTime earlierDate: mondayMorningClosing && [aktuelleZeit laterDate: mondayMorningOpening])
{
NSLog (@"The shop is open!");
}
else if ([actualTime earlierDate: mondayNoonClosing]&& [actualTime laterDate: mondayNoonOpening])
{
NSLog (@"The shop is open!");
}
else
{
NSLog (@"The shop is closed!");
}
But no matter what time it is, it´s always shown “The shop is open!”.
Maybe you have an idea what to do that i can show the opening status right…
You misunderstood the meaning of earlierDate and laterDate.The methods are not “isEarlierDate” and “isLaterDate”, they do not return a BOOL.These methods return a NSDate object, the earlier (or later) of the two dates.They’re not nil objects, so they’re always evaluated to true.
Compare the two dates taking it’s time interval: