Chris
Chris

Reputation: 941

How to correctly format a NSTimeInterval as a time period, respecting i18n?

Given an NSTimeInterval representing a period of time, how do I create a human-readable string that expresses that time period in a way that respects i18N?

For example, if the NSTimeInterval is equal to 10823.23435 seconds, then some reasonable ways of rendering that as a string for those in the US might be:

(I.e., stuff you'd see on a stopwatch.)

Presumably other locales might use different renderings.

I'm aware of NSDateFormatter and NSNumberFormatter, but it's not clear to me if or how to use one of these for this problem.

I'm also aware of FormatterKit, but that only does relative time periods (e.g., "3 minutes ago"). I'm looking for absolute time periods.

It is trivial to cook up my own locale-specific solution, but has this problem been solved properly already?

Thanks.

Upvotes: 5

Views: 722

Answers (1)

zaph
zaph

Reputation: 112857

For iOS 8 there are NSDateComponentsFormatter & NSDateIntervalFormatter, they may work for your application.

Docs are lacking see NSHipster: NSFormatter
and
Foundation/NSDateIntervalFormatter.h

Upvotes: 7

Related Questions