Is there some functionality in Cocoa to display ti

2020-03-28 04:41发布

问题:

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?

回答1:

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



回答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.



回答3:

See -[NSDateFormatter setDoesRelativeDateFormatting:].



回答4:

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