开发者

Nsdate function: between Objective c

开发者 https://www.devze.com 2023-04-11 19:58 出处:网络
I have such a task here Write a 开发者_JAVA百科function: between? (Time, start, end), where time, start, end: time in format \"hh: mm\"

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消