Question

A long time ago I watched a WWDC session video where Apple introduced an API which would print a time interval in a humanly readable, localized form.

Instead of printing a date when someone posted a comment, it was very easy to print a localized string such as "2 hours ago" or "Yesterday" or "Last week".

I can't find that video anymore and no references or examples for this API. I'm sure that it was a dedicated API to directly get this kind of localized strings for date intervals relative to the current date.

Was it helpful?

Solution

I can't remember the video (although there was some video about it) but I think the API you are thinking of is:

[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle]

I'm not sure if this API does relative date formatting and the frameworks mentioned by Midhun MP take this a lot further

You can, of course, construct your own NSDateFormatter and set the setDoesRelativeDateFormatting:YES property to provide text like "Today" or "Tomorrow".

Example code is in the class reference below in setDoesRelativeDateFormatting:.

See NSDateFormatterClassReference

OTHER TIPS

I don't know about that video.

There is an open source library available for doing this: SEHumanizedTimeDiff.

Also check Three20's NSDateAdditions.

I think you're after session 244 from 2012.

Called "Internationalisation Tips and Tricks".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top