sandy - 7 months ago 54

C Question

I would like to calculate the no of days,hours,minutes between two different dates in iPhone(Objective C). Please provide any code sample to do the same.

Thanks

Sandeep

Answer

If your dates are in NSDate objects, you can use the timeIntervalSinceDate method to find the difference as an NSTimeInterval, which will give you the difference in seconds.

You can then convert seconds to days, hours and minutes via a simple algorithm (this is off the top of my head and not tested):

```
minutes = seconds / 60;
seconds %= 60;
hours = minutes / 60;
minutes %= 60;
days = hours / 24;
hours %= 24;
```

Source (Stackoverflow)