0

What is the best way to find out if a date falls within a certain date range? I am doing this right now:

if (([rangeStart compare:userEnteredDate] == NSOrderedAscending) && ([rangeEnd compare:userEnteredDate] == NSOrderedDescending)

The only problem with the above code is that if the rangeStart date and the userEnteredDate are the same, the if statement would yield "false" for some reason unknown to me. Anyone have any ideas why?

Is there a better way to check if userEnteredDate falls within rangeStart and rangeEnd?

4

2 に答える 2

2

その理由は、rangeStartが に等しい場合userEnteredDate、ブール式の最初のチャンクが に評価されるためです。

NSOrderedSame == NSOrderedAscending

これは確かに誤りです。

順序を並べ替えて、次のようなものを使用します

if (([userEnteredDate compare:rangeStart] != NSOrderedDescending)
     && ([userEnteredDate compare:rangeEnd] != NSOrderedAscending)) {
   //... 
}
于 2013-04-14T00:10:23.547 に答える
0

もっと簡単な方法があるかどうかはわかりませんが、これはうまくいくはずです.-

if (([rangeStart compare:userEnteredDate] == NSOrderedAscending || 
     [rangeStart compare:userEnteredDate] == NSOrderedSame) && 
     ([rangeEnd compare:userEnteredDate] == NSOrderedDescending ||
      [rangeEnd compare:userEnteredDate] == NSOrderedSame))
于 2013-04-14T00:11:05.150 に答える