I have such a task here
Write a 开发者_JAVA百科function: between? (Time, start, end), where time, start, end: time in format "hh: mm"
for example, "23:15". The function should return TRUE, if "Time" falls between the 'Start' and 'End'. Case End < Start, for example ("23:11" "8:05") should be considered as: from 'Start' to 'End' the next day.
I have done so
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"HH:mm";
NSDate * time = [dateFormatter dateFromString:@"24:00"];
NSDate * start = [dateFormatter dateFromString:@"23:50"];
NSDate * end = [dateFormatter dateFromString:@"03:00"];
if(start<time)
{
if (end>time)
{
NSLog(@"YES");
}
}
but how do a background check In order to test the next day? as in the example, have any ideas?
I guess what you could do is verify if end < start
and add +24 hours to end
in that case. Then just verifying if time > start && time < end
will solve it.
精彩评论