5

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:

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

(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.

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
Chris
  • 941
  • 9
  • 18
  • Potential dublicate of http://stackoverflow.com/questions/8128083/nstimeinterval-formatting – David Oct 07 '14 at 11:26
  • Thanks, @David, but that solution uses formatting that assumes a specific formatting of time periods, rather than producing strings for the user's locale. I'll likely fall back on that if nothing else suits… – Chris Oct 07 '14 at 13:23
  • @Zaph: I'm not seeing those classes. Link? – Chris Oct 07 '14 at 13:28
  • @Chris It's not documented yet. We can see their name in [NSFormatter reference](https://developer.apple.com/LIBRARY/IOS/documentation/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/index.html) "Inheritance" section. As of now, just type it in the editor and "Jump to Definition" – rintaro Oct 07 '14 at 14:30

1 Answers1

7

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

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

zaph
  • 111,848
  • 21
  • 189
  • 228