sandy sandy - 1 month ago 7
C Question

How to get difference between two days in iPhone

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;