How to properly format NSTimeInterval as a time period considering i18n?

Given an NSTimeInterval representing a time period, how do I create a user-readable string that expresses this time period in a way that matches i18N?

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

  • 3:23
  • 3 hours, 23 seconds
  • and etc.

(Ie, the material you'll see on the stopwatch.)

Presumably, other locales may use different visualizations.

I know NSDateFormatter and NSNumberFormatter, but I don’t understand if and how to use one of them for this problem.

I also know FormatterKit, but these are only relative time periods (for example, "3 minutes ago"). I am looking for absolute periods of time.

It is trivial to prepare your own locale-specific solution, but has this problem been solved properly already?

Thank.

+4
source share
1 answer

For iOS 8, there are NSDateComponentsFormatter and NSDateIntervalFormatter, they can work for your application.

No documents. NSHipster: NSFormatter
and
Foundation / NSDateIntervalFormatter.h

+6
source

All Articles