GitHub Repository: https://github.com/MattMcComb/RelativeDateDescriptor
A side-effect of developing apps over a long period of time is that you begin to see common problems that must be solved in numerous apps. Describing dates is just one such example. When I wrote my first app Simple2Do I needed a way to desribe to user’s when an item from their to-do list was due. I found the most comprehendible way to achieve this was to describe the time to/from the task in a single unit – i.e. the number of hours, minutes, seconds, days, months or years until the task was due.
Fast forward a couple of projects and I began work on a Twitter client which displayed the user’s timeline. Again I wanted an easy way for the user to visualise when something happened, in this case when a tweet appeared in their timeline.
The second time I encountered this problem I decided to code up a little utility class for creating these descriptions. The result was RelativeDateDescriptor, an objective-c utility class for producing human readable descriptions of date intervals which is now available open-license on Gitub.
Here’s an extract from the project README with some examples…
RelativeDateDescriptor *descriptor = [[RelativeDateDescriptor alloc] initWithPriorDateDescriptionFormat:@"%@ ago" postDateDescriptionFormat:@"in %@"]; // date1: 1st January 2000, 00:00:00 // date2: 6th January 2000, 00:00:00 [descriptor describeDate:date2 relativeTo:date1]; // Returns '5 days ago' [descriptor describeDate:date1 relativeTo:date2]; // Returns 'in 5 days' // date1: 1st January 2000, 00:00:00 // date2: 1st January 2000, 00:00:18 [descriptor describeDate:date2 relativeTo:date1]; // Returns '18 seconds ago' descriptor describeDate:date1 relativeTo:date2]; // Returns 'in 18 seconds'