Mail.app style relative date formatter.
Note: This repo will be deprecated in favour of RelativeDateFormatter some time in the future.
en_GB | en_US | |
---|---|---|
Same Day | 13:45 | 1:45 PM |
Yesterday | Yesterday | Yesterday |
Same Week | Monday | Monday |
Same Year | 15 Mar | Mar 15 |
Last Year | 22/04/2013 | 4/22/13 |
NSDate *date = ...;
TFGRelativeDateFormatter *formatter = [[TFGRelativeDateFormatter alloc] init];
NSString *relativeString = [formatter stringForDate:date];
TFGRelativeDateFormatter
has similar cavets to a NSDateFormatter
(it is used
internally) meaning its a good idea to cache instances and not modify the locale.
A handy class method +sharedFormatter
is provided to get around this for the
common case where locale is never changed.
NSDate *date = ...;
NSString *relativeString = [[TFGRelativeDateFormatter sharedFormatter] stringForDate:date];
Thread Safety: TFGRelativeDateFormatter
is fully thread safe on iOS 7 and
OS X 10.9 (Mordern 64-bit apps). Earlier versions are not however and its recommended
to only use an instance per thread. (More information about thread safety can be
found by reading the NSDateFormatter class reference.)
TFGRelativeDateFormatter is available through CocoaPods, to install it simply add the following line to your Podfile:
pod 'TFGRelativeDateFormatter'
Alternatively, simply drag TFGRelativeDateFormatter.h
and TFGRelativeDateFormatter.m
to your project.
TFGRelativeDateFormatter is available under the MIT license. See the LICENSE file for more info.