开发者

date in a specific range with NSOrderedAscending and NSOrderedDescending

I have this code:

NSComparisonResult compareStart = [firstDate compare: dateSelected]; 
NSComparisonResult compareEnd = [secondDate compare: dateSelected];

if (((compareStart == NSOrderedAscending) || (compareStart == NSOrderedSame))
                   && (compareEnd == NSOrderedDescending)) 

but it don't entry in the "if" when firstDate = dateSelected or se开发者_运维知识库condDate = dateSelected...why??


Are you sure that when firstDate equals dateSelected that date selected is earlier than secondDate? Currently your logic is: firstDate <= dateSelected < secondDate. If you want firstDate <= dateSelected <= secondDate, then you need to implement the code as follows:

NSComparisonResult compareStart = [firstDate compare: dateSelected]; 
NSComparisonResult compareEnd = [secondDate compare: dateSelected];

if ( (compareStart == NSOrderedAscending || compareStart == NSOrderedSame)
                   && (compareEnd == NSOrderedDescending || compareEnd == NSOrderedSame)) {
  // some code
}

Update:

To compare two NSDates without relying on time see: Comparing two NSDates and ignoring the time component

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜