2

What I am searching for is a Cocoa (or third party) class that can display time intervals in natural language, e.g.:

  • 10 seconds ago
  • 1 hour ago
  • 2 days ago

Do you know anything that could help me to achieve this task without writing it by myself and melting in if-else hell?

Rafael Bugajewski
  • 1,702
  • 3
  • 22
  • 37

4 Answers4

6

The one I've originally pointed to does not seem to be there anymore but as pointed out there is NSDate-TimeAgo, I haven't tried it but looks good to me.

EDIT: updated answer to point to existing code

stefanB
  • 77,323
  • 27
  • 116
  • 141
2

Your question for Cocoa is the same as this question for Cocoa Touch, and the answer to that one will work for you as well, because it uses Foundation, which is in both Cocoa and Cocoa Touch.

Community
  • 1
  • 1
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • You don’t really answer my question. All of the proposed solutions in the thread you mention still have to deal with a couple of if-else statements. But thanks for pointing out the thread anyway. – Rafael Bugajewski Feb 02 '10 at 16:48
  • The `if (component != 0) /*add component to string*/` statements have to be *somewhere* anyway; how is that a problem? If that's not what you're talking about, then what if statements are you talking about? – Peter Hosey Feb 02 '10 at 19:47
  • I just searched for third party stuff that does this for me, so I don’t have to. – Rafael Bugajewski Feb 05 '10 at 18:18
1

See -[NSDateFormatter setDoesRelativeDateFormatting:].

Ken
  • 12,933
  • 4
  • 29
  • 32
  • that only offers very limited options – stefanB Feb 02 '10 at 02:30
  • Mm, well, recall that you have the rest of the NSDateFormatter API to work with as well. The main limitation I see is that it always formats with respect to the current date. – Ken Feb 02 '10 at 03:17
1

I can really recommend Mattt Thompson's TTTTimeIntervalFormatter - very clean, very functional (lots of other super useful bits and pieces in the same repo).

Zoë Smith
  • 1,084
  • 1
  • 11
  • 19