0

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.

Proud Member
  • 40,078
  • 47
  • 146
  • 231

3 Answers3

3

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

Robotic Cat
  • 5,899
  • 4
  • 41
  • 58
1

I don't know about that video.

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

Also check Three20's NSDateAdditions.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
1

I think you're after session 244 from 2012.

Called "Internationalisation Tips and Tricks".

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Unfortunately doesn't address this issue. Must have been another session. People were chanting and applauding to it. – Proud Member Jan 21 '13 at 16:14
  • Ah, it might have been a keynote thing then. A "reveal" or "sneak peek" rather than a WWDC. People tend to cheer more at them. – Fogmeister Jan 21 '13 at 16:30