1

I am having trouble calculating time difference. I am getting one time from web service response 18:40:56 in this format. I need to find the current timing of Australia, i have also find that, now how do i subtract the both the timing in hour:minute:second format and display the result.

user1237473
  • 69
  • 2
  • 8
  • 2
    This sort of question is not suitable for StackOverflow. Please read the Date and Time Programming Guide for iOS (https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html) and update your question with code and specific issues if you still need help afterward. – Conrad Shultz Mar 05 '12 at 08:12
  • possible duplicate of [How to Get time difference in iPhone](http://stackoverflow.com/questions/1427151/how-to-get-time-difference-in-iphone) and similar to many other questions. – Abizern Mar 05 '12 at 08:17

3 Answers3

3
NSTimeInterval secondsBetween = [firstDate timeIntervalSinceDate:secondDate];
Ashraf
  • 2,612
  • 1
  • 20
  • 35
1

For calculating difference between 2 times, get the timestamp in seconds of both times, and get the difference:

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"HH:mm:ss"];

NSDate *myDate = [format dateFromString: myDateAsAStringValue];

Now, you convert it to timestamp:

 [myDate timeIntervalSince1970];

Do that with both and sustract them

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
0
NSTimeInterval timeDifference = [firstDate timeIntervalSinceDate:secondDate];

double minutes = timeDifference / 60;

double hours = minutes / 60;

double seconds = timeDifference;

double days = minutes / 1440;

NSLog(@"%.0f, %.0f, %.0f, %.0f", days, hours, minutes, seconds);
Hiren
  • 12,720
  • 7
  • 52
  • 72
hayden
  • 293
  • 1
  • 5
  • Thanks for the reply however i have previously done this, i have one issue to find difference of time, like if i have one time 18:53:10 and another time 14:26:12, so need to find difference between them in hour and minutes. – user1237473 Mar 05 '12 at 10:09