0

I not understand where i'm wrong to getting correct difference of two dates,

-(void)updateTimeLabel{
    NSString *serverResponseTimeString = @"02-08-2014 05:26:32";
    NSString *systemDateString = @"02-08-2014 05:31:46";
    NSDate *serverTime = [self dateFromWebStringResponse:serverResponseTimeString];
    NSDate *systemDate = [self dateFromWebStringResponse:systemDateString];

    NSLog(@"serverResponseTime %@", serverResponseTimeString);
    NSLog(@"serverTime %@", serverTime);
    NSLog(@"systemDate %@", systemDate);
    NSLog(@"difference of time %d", [self differenceOfTimeInterval:systemDate toDate:serverTime]);
}

-(NSDate *)dateFromWebStringResponse:(NSString *)dateInString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm-dd-yyyy hh:mm:ss"];
    NSDate *theDate = [dateFormatter dateFromString:dateInString];
    return theDate;
}


-(NSInteger)differenceOfTimeInterval:(NSDate *)fromDate toDate:(NSDate *)toDate{
    NSTimeInterval differenceOfIntervals = [toDate timeIntervalSinceDate: fromDate];
    NSInteger minutes = floor(differenceOfIntervals/60);
    return minutes;
}

Output :-

2014-02-08 17:58:57.185 iOSPractise[16770:907] serverResponseTime 02-08-2014 05:26:32
2014-02-08 17:58:57.186 iOSPractise[16770:907] serverTime 2014-01-07 23:56:32 +0000
2014-02-08 17:58:57.187 iOSPractise[16770:907] systemDate 2014-01-07 22:00:46 +0000
2014-02-08 17:58:57.188 iOSPractise[16770:907] difference of time 115 

I expect 5 minutes difference.

Tirth
  • 7,801
  • 9
  • 55
  • 88

1 Answers1

2

Your date format is wrong. The format for months is "MM" (not "mm", which are the minutes), and the 24-hour format is "HH" ("hh" is for the 12-hour am/pm hours):

[dateFormatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];

Also, as @Flexicoder mentioned, you should specify a "locale" to be independent of the user's locale settings:

[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

(See http://www.flexicoder.com/blog/index.php/2013/10/ios-24-hour-date-format/ or What is the best way to deal with the NSDateFormatter locale "feechur"?.)

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 2
    Its worth noting as well that if the user has their phone to 12 hour HH will not return what you expect http://www.flexicoder.com/blog/index.php/2013/10/ios-24-hour-date-format/ – Flexicoder Feb 08 '14 at 12:43
  • Same difference 115 that means 1.9 minutes. It should be more 4 minutes right? – Tirth Feb 08 '14 at 12:45
  • @Flexicoder: Your are completely right, thanks for reminding me and the link. – Martin R Feb 08 '14 at 12:48
  • @Reformer: I have run the code and got (-314) seconds or (-6) minutes as result, which looks correct. But see updated answer with the important information from Flexicoder about setting the locate. Does that help? – Martin R Feb 08 '14 at 12:52
  • @MartinR, yes your code working properly. I convert server time string to NSDate using given dateformatter (with NSLocal). Now i pass servertime NSDate and [NSDate date] to calculate difference it give me always 0. Should i convert [NSDate date] into given dateformatter? – Tirth Feb 08 '14 at 13:14
  • @Reformer: No, you should compare NSDate objects (as you currently do). What are the values of fromDate and toDate? – Martin R Feb 08 '14 at 13:20
  • fromDate is my server time which is i convert into NSDate. And toDate is [NSDate date] simple current system date. – Tirth Feb 08 '14 at 13:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47086/discussion-between-martin-r-and-reformer) – Martin R Feb 08 '14 at 13:23