Robin
Robin

Reputation: 1

How to calculate the passed time and return it descriptively?

I'd like to know the passed time in between two dates, for example: date1 and date2, I'd need a function like getPassedTime(date1, date2).

The function result should be a string like '1 day 3 hours 12 minutes 10 seconds' or just '40 seconds'.

I know that the method timeIntervalSinceDate of the class NSDate can be used, but the problem is converting it to a detailed/descriptive string that could easily be understood.

Can anyone help? Sharing some code or tips will be appreciated.

Upvotes: 0

Views: 131

Answers (1)

drawnonward
drawnonward

Reputation: 53679

Look at NSCalendar:

- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts;

You will still have to decide which components to display, but they will be broken out by unit.

Upvotes: 1

Related Questions