Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative Date Formatting, output for past dates?

I am looking at using -[NSDateFormatter setDoesRelativeDateFormatting:] to present dates as "Today" or "Yesterday". I am only looking at dates in the past but am curious what options I would see localised for the UK.

Just

  • "Today"
  • "Yesterday"

or anything more convoluted like

  • "The day before yesterday"

Are the possible outputs listed anywhere so I can get an idea of the screen space needed to correctly display them?

like image 408
fuzzygoat Avatar asked Aug 19 '11 15:08

fuzzygoat


1 Answers

Yes. They are listed in your console window when you run the following program:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSDateFormatter * formatter = [[NSDateFormatter  alloc] init];
        [formatter setDateStyle:NSDateFormatterFullStyle];
        [formatter setDoesRelativeDateFormatting:YES];

        NSCalendar * cal = [[NSLocale currentLocale] objectForKey:NSLocaleCalendar];
        NSDateComponents * minusOneDay = [[NSDateComponents alloc] init];
        [minusOneDay setDay:-1];
        NSDate * today = [NSDate date];
        NSDate * date = [NSDate date];

        while( 1 > [[cal components:NSYearCalendarUnit fromDate:date toDate:today options:0] year] ){

            NSLog(@"%@", [formatter stringFromDate:date]);
            date = [cal dateByAddingComponents:minusOneDay
                                        toDate:date
                                       options:0];
        }

    }
    return 0;
}

In my locale, the list seems to just be "Tomorrow", "Today", and "Yesterday".

like image 130
jscs Avatar answered Sep 20 '22 18:09

jscs