如何比较目标C中的两个NSDate对象 [英] How to compare two NSDate objects in objective C
问题描述
我有Date类型的对象。
所以我想比较他们,我写了一个if条件下面的方式
if(([startDate1 isEqualToDate:[self getDefaultDate]])||(startDate1!= [self getDefaultDate]&& m_selectedDate> = m_currentDate1&& cycleStopped))
$所以,无论我是对还是错在这种方法?
{
///执行某些条件
}
还有一件事,无论是否这种方式是正确的
if(dtdate> [m_array objectatIndex:i]
{
}
因为我得到了一个随机行为..
$ b b请帮助我的朋友和极客。
尊重
Ranjit方案下面是使用
NSDate
方法,compare:
if([startDate compare:endDate] == NSOrderedDescending)//如果开始时间晚于结束
{
/ / do something
}
从类引用:
如果...
接收者和anotherDate完全相同,
NSOrderedSame。
接收者的时间晚于另一个日期NSOrderedDescending。
接收者的时间早于anotherDate, NSOrderedAscending。
您也可以使用
timeIntervalSinceDate:
比较两个日期并找出它们之间的秒数。
编辑:
if(([startDate1 compare:[self getDefaultDate]] == NSOrderedSame)|| ([startDate1 compare:[self getDefaultDate]]!= NSOrderedSame&(([m_selectedDate compare:m_currentDate1] == NSOrderedDescending)|| [m_selectedDate compare:m_currentDate1] == NSOrderedSame)&& cycleStopped))
I have objects of Date type..
So I want to compare them, and I have written a if condition in the following below way
if(([startDate1 isEqualToDate:[self getDefaultDate]]) || (startDate1 != [ self getDefaultDate] && m_selectedDate >= m_currentDate1 && cycleStopped)) { ///execute some condition }
So whether I am right or wrong..in this approach?
One more thing whether righting this way is right
if(dtdate > [m_array objectatIndex:i] { }
Because I am getting a random behaviour..
So please help me out friends and geeks
Regards Ranjit
解决方案Here's an example using the
NSDate
method,compare:
if([startDate compare: endDate] == NSOrderedDescending) // if start is later in time than end { // do something }
From the class reference:
If...
The receiver and anotherDate are exactly equal to each other, NSOrderedSame.
The receiver is later in time than anotherDate, NSOrderedDescending.
The receiver is earlier in time than anotherDate, NSOrderedAscending.
You could also use the
timeIntervalSinceDate:
method if you wanted to compare two dates and find the seconds between them, for example.EDIT:
if(([startDate1 compare:[self getDefaultDate]] == NSOrderedSame) || ([startDate1 compare: [self getDefaultDate]] != NSOrderedSame && (([m_selectedDate compare: m_currentDate1] == NSOrderedDescending) || [m_selectedDate compare: m_currentDate1] == NSOrderedSame) && cycleStopped))
这篇关于如何比较目标C中的两个NSDate对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!