-1

I am comparing two dates in this way:

NSDateFormatter *df= [[NSDateFormatter alloc] init];
[df setDateFormat:@"  dd : MM : yyyy"];

NSDate *dt1 = [[NSDate alloc] init];
dt1=[df dateFromString:TheDate];

NSDate *Date=[NSDate date];
NSLog (@"DATE CURRENT: %@ DATE 2 %@", Date, dt1);

if ([Date compare:dt1] == NSOrderedDescending) {
    NSLog(@"Date is later than date2");

} else if ([Date compare:dt1] == NSOrderedAscending) {
    NSLog(@"Date is earlier than date2");
    [postsToBeDeleted addObject: textmessage];

} else {
    NSLog(@"dates are the same");
}

The NSLog gives me:

DATE CURRENT: 2013-02-05 21:37:54 +0000 DATE 2 2012-01-04 23:00:00 +0000

But it is absolutely clear that the current date is far ahead of the dt1 date, and still I get NSLog(@"Date is later than date2");. Why is this?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
user2014474
  • 1,117
  • 5
  • 19
  • 36

2 Answers2

2

Because of how -compare: works for NSDate instances. The docs say:

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.

In this case, the receiver is Date, and anotherDate is your dt1. Date is later in time than dt1, so you get NSOrderedDescending. (This is actually the situation your log statements imply; I'm a little uncertain as to where the confusion is.)

Tim
  • 59,527
  • 19
  • 156
  • 165
  • And so,how can I compare not the time but the actual date? Day, month, year? If I understood correctly NSOrdered... Refers to the time and not the date? – user2014474 Feb 05 '13 at 21:49
  • @user2014474, either create dates with same time, or create date componetnts without times – vikingosegundo Feb 05 '13 at 21:49
  • @user2014474, what do u mean? – vikingosegundo Feb 05 '13 at 21:51
  • Should I replace the compare with erlier date? And later date? – user2014474 Feb 05 '13 at 21:52
  • @user2014474: those orders refer to the **entire date objects**, day, month, and year included. Anything in 2013 will compare NSOrderedDescending to anything in 2012. If you want to compare **just times**, maybe take a look at NSDateComponents? – Tim Feb 05 '13 at 21:52
  • If I want to remove the time, which I don'd need, will that compare the actual date? – user2014474 Feb 05 '13 at 21:52
  • 1
    @user2014474, you should check the documents and existing threads for how to create dates with identical times or how to create NSDateComponents. – vikingosegundo Feb 05 '13 at 21:53
  • Ok, but it is clear that I don't want times, only the date... So i'll google: how to remove NSDate time – user2014474 Feb 05 '13 at 21:55
  • @user2014474, YEAH, that's a start! – vikingosegundo Feb 05 '13 at 21:55
  • @user2014474: I'm sorry, that wasn't clear to me. I think you're on the right track now, though! – Tim Feb 05 '13 at 21:56
  • @user2014474, but actually you should be aware, that you dont want to remove the time but set it to the same. and just out of experience: choose a time around noon. than you avoid daylight saving time and leap second gotchas. – vikingosegundo Feb 05 '13 at 21:57
  • Their's only 1 problem. I can't set the time for the current date because its the real time... – user2014474 Feb 05 '13 at 22:00
  • Mmmmm I found something interesting here http://stackoverflow.com/questions/3916392/removing-time-components-from-an-nsdate-object-using-objective-c-cocoa – user2014474 Feb 05 '13 at 22:01
  • the day, that no-one is asking for comparison of nsdate objects, [this site](http://hasthelargehadroncolliderdestroyedtheworldyet.com) will return **YUP.** – vikingosegundo Feb 05 '13 at 22:05
0

please let me quote you some interesting thing:

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.
holex
  • 23,961
  • 7
  • 62
  • 76