sandy sandy - 1 year ago 82
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.


Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download