ios - 如何比较 objective-c 中的两个 NSDate 对象

标签 ios nsdate

我有日期类型的对象。

我想比较一下,我写了一个if条件如下:

if (([startDate1 isEqualToDate:[self getDefaultDate]]) || (startDate1 != [ self getDefaultDate] && m_selectedDate >= m_currentDate1 && cycleStopped))
 {

///execute some condition

}

我的这种做法是对还是错?

还有一件事。这样对吗:

if (dtdate > [m_array objectatIndex:i]
{

}

因为我得到了随机行为。

最佳答案

下面是一个使用 NSDate 方法的例子,compare:

if([startDate compare: endDate] == NSOrderedDescending) // if start is later in time than end
{
    // do something
}

来自类引用:

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.

例如,如果您想比较两个日期并找出它们之间的秒数,您也可以使用 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))

关于ios - 如何比较 objective-c 中的两个 NSDate 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8183472/

相关文章:

iphone - 如何让我的 UITableView Controller 填充?

ios - 背景音频不会播放

ios - 向 NSDate 添加小时数不起作用?

ios - 如何从 NSDate 获得 1 小时的 future 时差

ios - UITableView、CollectionView 还是 AutoLayout?

ios - 核心数据错误 SIGABRT : keypath AttributeName not found in entity <NSSQLEntity EntityName id=2>

ios - Swift:加载后 NSDate 失去亚秒级精度

cocoa - 当 runloop 被阻塞时 NSTimer 不会触发

iphone - 升级到 iOS 5

ios - 如何在iOS中按创建日期对文档目录中的文件进行排序?