问题描述
我想比较日期,并使用此代码
I want to compare dates and I use this code
在一个示例中,我的日期的日期值为:
date1 = 6/06/2011
date2 = 8/06/2011
in one example date values for my date are:date1 = 6/06/2011date2 = 8/06/2011
如果dateSelected = 7/06/2011没关系,但是如果dateSelected = 6/06/2011或dateSelected = 8 / 06/2011代码没有在我的如果中输入,为什么???
if dateSelected = 7/06/2011 it's all ok, but if dateSelected = 6/06/2011 or dateSelected = 8/06/2011 the code don't entry inside my "if", why???
if (([dateSelected compare:date1] == NSOrderedDescending) &&
([dateSelected compare:date2]== NSOrderedAscending))
{}
推荐答案
NSDates表示自2000年1月1日00:00 UTC开始的时间点。因此,所有这些日期都有时间成分。 -比较:
比较日期和时间。
NSDates represent a point in time since 1/1/2000 00:00 UTC. So all of those dates have time components. -compare:
compares date and time.
想必您真的想检查所选日期是否在6/6/2011 00:00到9/6/2011 00:00之间。另外,您可能希望日期6/6/2011 00:00计入该范围内。因此,您需要类似
Presumably, you really want to check if the date selected is between 6/6/2011 00:00 and 9/6/2011 00:00. Also you probably want the date 6/6/2011 00:00 to count as in the range. So you need something like
NSComparisonResult compareStart = [date1 compare: selectedDate]; // date1 is 6/6/2011 00:00
NSComparisonResult compareEnd = [date2 compare: selectedDate]; // date2 is 9/6/2011 00:00
if ((compareStart == NSOrderedAscending) || (compareStart == NSOrderedSame)
&& (compareEnd == NSOrderedDescending))
{
// date is in the right range
}
这篇关于iOS:NSOrderedDescending和NSOrderedAscending问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!