Cocoa Tutorial: Yesterday, Today, and Tomorrow with NSDate
by Brian Dunagan
One common need is to make dates within the last week more readable, like using “Today” instead of “September 13”. Apple Mail and NetNewsWire both do this. When using Cocoa Bindings, the easiest path to this is through an NSValueTransformer. I initially tried to use NSCalendarDate (following this example), but apparently Apple might deprecate that object in 10.6. So instead I went with NSDateComponents.
Below is code from BDDateTransformer. It converts appropriate dates into “Tomorrow”, “Today”, “Yesterday”, and the past five days of the week. Feel free to use this wherever.
07/31/2009 UPDATE: I finally added Ford’s code for listing a time for today, like the iPhone’s Mail. I also updated RssBucket on Google Code, where I use this date transformer in context.