0

I am comparing two NSDates called serverDate and localDate. Despite both dates outputting to be the same time, I continue to get that serverDate is show to be more recent than localDate.

Here is my code for comparison:

if ([serverDate timeIntervalSinceReferenceDate] > [localDate timeIntervalSinceReferenceDate]) {
        NSLog(@"server date more recent");

    } else if ([serverDate timeIntervalSinceReferenceDate] <= [localDate timeIntervalSinceReferenceDate]){
        NSLog(@"local date more recent");

    }

I have used extremely precise NSDateFormatter to display the date as "yyyy-MM-dd HH:mm:ss:SSSSSSSSSSZ", which outputs the following: Server date: 2014-09-17 23:20:02:5090000000-0700, local date: 2014-09-17 23:20:02:5090000000-0700. But even then, serverDate continue to be regarded as the more recent date.

What am I doing incorrectly? Thanks for your input.

daspianist
  • 5,336
  • 8
  • 50
  • 94
  • `NSLog(@"%f, %f", [serverDate timeIntervalSinceReferenceDate], [localDate timeIntervalSinceReferenceDate])` What would you get when you log it out. – KudoCC Sep 18 '14 at 06:45
  • Good call @KudoCC! When I log it out I get `432714002.509000, 432714002.508961` for the two dates respectively. I guess if there is a way that I could round the nearest 1/10 of a second would be the best solution. – daspianist Sep 18 '14 at 06:47
  • You also don't need `else if` as `else` will have the same effect. – trojanfoe Sep 18 '14 at 06:47
  • True that about the `else if`. Will change in code. – daspianist Sep 18 '14 at 06:48
  • Check this - http://stackoverflow.com/questions/18465502/ios-compare-two-less-or-equal-nsdates/18465596#18465596 – Kumar KL Sep 18 '14 at 06:50
  • I'm curious to know what is the result if you use `[serverDate compare:localDate]` or `[serverDate timeIntervalSinceDate:localDate]` to compare. – KudoCC Sep 18 '14 at 07:12

2 Answers2

2

You should not use NSimeIntervell as NSDate Has Compare Function for comparing dates use compare function instead

if ([serverDate  compare:localDate ] == NSOrderedDescending) {
    NSLog(@"serverDate is later than localDate");
} else if ([serverDate  compare:localDate] == NSOrderedAscending) {
    NSLog(@"serverDate is earlier than localDate");
} else {
    NSLog(@"dates are the same");
}

Hope this helps

Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
  • If [this](https://code.google.com/p/cocotron/source/browse/Foundation/NSDate/NSDate.m?r=83f4dcd06e67963108a3ee0f9942a6609ad35edf) is anything to go by, `NSDate` uses the `NSTimeInterval` for comparison anyway. – trojanfoe Sep 18 '14 at 06:53
  • Agree but why not use something that iOS provides as property. – Bhumit Mehta Sep 18 '14 at 06:57
  • 1
    I don't disagree; my comment was for interest only. – trojanfoe Sep 18 '14 at 06:57
1

This will help you it works for me

if ([serverDate isEqualToDate: localDate])
    {
        NSLog(@"Date are same");
    }else
{
 NSLog(@"Date are not equal")
}
dheeru
  • 395
  • 3
  • 13
  • Thanks for adding your answer. While `isEqualToDate` is great, what I would really like to be able to tell is whether if one date is later than the other date. I guess using `>` or `<` would not be ideal here? – daspianist Sep 18 '14 at 07:00
  • this will help you if ([serverDate compare:localDate] == -1) { NSLog(@"serverDate is later than localDate"); } else if ([serverDate compare:localDate] == 1) { NSLog(@"serverDate is earlier than localDate"); } else { NSLog(@"dates are the same"); } – dheeru Sep 18 '14 at 08:08