Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 比较日期和时间对象_Ios_Iphone_Objective C - Fatal编程技术网

Ios 比较日期和时间对象

Ios 比较日期和时间对象,ios,iphone,objective-c,Ios,Iphone,Objective C,我使用的是一种方法,它没有给我正确的输出,然后我才知道,我比较日期和时间对象的方式是完全错误的。我在谷歌上搜索,但找不到相关的解决方案。我想将日期和时间对象与另一个对象进行比较 例如: if (dtcur >= dtstart && dtcur <= myDT) // 2014-03-24 11:30:00 >= 2014-03-24 11:00:00 & 2014-03-24 11:30:00 <= 2014-03-25 11:00:00

我使用的是一种方法,它没有给我正确的输出,然后我才知道,我比较日期和时间对象的方式是完全错误的。我在谷歌上搜索,但找不到相关的解决方案。我想将日期和时间对象与另一个对象进行比较

例如:

  if (dtcur >= dtstart && dtcur <= myDT) // 2014-03-24 11:30:00 >= 2014-03-24 11:00:00 & 2014-03-24 11:30:00 <= 2014-03-25 11:00:00
  {
     // should come here in if condition
  }

if(dtcur>=dtstart&&dtcur=2014-03-24 11:00:00&2014-03-24 11:30:00您是否引用了NSDate??如果是这样,您可以使用属性TimeIntervalnce1970(双值)进行比较。您不能直接比较NSDate,因为它们是对象。下面是一个小示例。您可以取消注释该行以查看结果如何更改

NSDate *date1 = [NSDate date];
NSDate *date2 = [date1 dateByAddingTimeInterval:10];

if (date2.timeIntervalSince1970 > date1.timeIntervalSince1970)
{
 NSLog(@"date 2 is bigger");
}
else
{
NSLog(@"date 1 is bigger");
}

许多对象,包括NSDate,都有一个
compare:
方法。请像这样使用它:

if ([date1 compare:date2] == NSOrderedAscending) {
  // date2 is after date1
} else if ([date1 compare:date2] == NSOrderedDescending) {
  // date2 is before date1
} else if ([date1 compare:date2] == NSOrderedSame) {
  // they're both the same date
}

看,这可能会对你有所帮助,这不只是为了日期比较吗?我想把日期和时间与另一个日期和时间进行比较,例如,如果(dateTimeObj1)你确定不可能比较两个NSDate对象吗?在写这些错误的东西之前,你为什么不先阅读一下苹果的文档?为什么不在键入这些废话之前试着理解我所说的内容?如果你将nsobjects与(>,<,>=,嘿@nsuinteger,你错把它当成了这条消息的接收者。我从来没有把NSDate对象和基元运算符进行过比较。你使用
compare:
方法的解决方案并不是最好的性能。改用
后:
前:
方法。你的代码解决不了这个问题。你的代码没有我不能解决这个问题。在键入答案之前仔细阅读问题。@igormatyuskin?嗯?他想知道如何比较日期。这就是我的代码所做的。它已经起作用了。非常感谢:)
if ([date1 compare:date2] == NSOrderedAscending) {
  // date2 is after date1
} else if ([date1 compare:date2] == NSOrderedDescending) {
  // date2 is before date1
} else if ([date1 compare:date2] == NSOrderedSame) {
  // they're both the same date
}