0

I was working with a method and it was not giving me proper output then I came to know, the way I was comparing date and time object was completely wrong. I googled but could not find some relevant solution. I want to compare a date and time object with another object.

for example:

  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
  }

but it goes in else. It means my comparison is wrong, can anyone help me about how to compare two date and time objects. Thanks in advance

user3422986
  • 51
  • 2
  • 8
  • Look [here](http://stackoverflow.com/questions/5965044/how-to-compare-two-nsdates-which-is-more-recent). This may help you – Akhilrajtr Mar 24 '14 at 07:27
  • isn't it only for date comparison ? I want to compare date and time with another date an time for example if(dateTimeObj1 <=dateTimeObj2) { nslog (@"yes"); } where as dateAndTimeObj1 would be 2014-05-2014 12:31 – user3422986 Mar 24 '14 at 07:30

3 Answers3

0
BOOL isLaterOrEqualToStartDate = [dtcur isEqualToDate:dtstart] || [dtcur later:dtstart];
BOOL isEarlierOrEqualToMyDate = [dtcur isEqualToDate:myDT] || [dtcur earlier:myDT];

if(isLaterOrEqualToStartDate && isEarlierOrEqualToMyDate)
{
    // do something
}
Igor Matyushkin
  • 778
  • 4
  • 4
0

are you refering to NSDate ?? if so you can compare using the property timeIntervalSince1970 (which is a double value). you cannot directly compare NSDate as they are objects. Here is small example. you can uncomment the line to see how result changes .

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");
}
nsuinteger
  • 1,503
  • 12
  • 21
  • Are you sure that it's not possible to compare two NSDate objects? Why don't you read Apple's documentation before writing such wrong things? – Igor Matyushkin Mar 24 '14 at 07:42
  • why dont you try to understand the context of what I said before typing such nonsense ? if you compare nsobjects with ( > , < , >= , <= ) you will get undesired results. run it for yourself (several times) and see if you're so sure it works... NSDate has a compare: method which you can use otherwise easiest would be to use the method timeIntervalSince1970 which will give you a double value which you can compare the way he intended. – nsuinteger Mar 24 '14 at 07:59
  • hey @nsuinteger, you've mistaken with receiver of this message. I never compared NSDate objects with primitive operators. And your solution of using `compare:` method is not the best in kind of performance. Use `later:` and `earlier:` methods instead. – Igor Matyushkin Mar 24 '14 at 09:13
  • Your code doesn't solve the issue. – Igor Matyushkin Mar 24 '14 at 09:15
0

Many objects, including NSDate, have a compare: method. Use it like so:

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
}
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110