Relative Date Formatting, output for past dates?

大憨熊 提交于 2019-12-07 03:11:06

问题


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?


回答1:


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".



来源:https://stackoverflow.com/questions/7123911/relative-date-formatting-output-for-past-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!