2

I have two dates like startDate = 2016-02-24 10:25:11 +0000 and endDate = 2016-02-24 10:26:11 +0000.

I want to find time interval between these dates in seconds.

Thanks in advance.

psk
  • 162
  • 1
  • 13
  • i have tried this but it is giving me wrong calculation. When i made difference of 10 minutes between dates. It is giving me result of 5. I have tried this code - NSTimeInterval distanceBetweenDates = [sDate timeIntervalSinceDate:eDate]; double secondsInAnHour = 3600; NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour; – psk Feb 24 '16 at 05:10

1 Answers1

3

Hope this can help you.

NSString *startDate = @"2016-02-24 10:25:11 +0000";
NSString *endDate = @"2016-02-24 10:26:11 +0000";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *start = [formatter dateFromString:startDate];
NSDate *end = [formatter dateFromString:endDate];

NSTimeInterval interval = [end timeIntervalSinceDate:start];
NSLog(@"time interval:%f", interval);
Justlike
  • 1,476
  • 10
  • 18