我正在使用 - [NSDateFormatter setDoesRelativeDateFormatting:] 将日期显示为今天或昨天。我只是查看过去的日期,但很好奇我会为英国本地化的选项。
只是
- 今天
- 昨天
- 前一天
b $ b
是否可能的输出列在任何地方,所以我可以了解正确显示它们所需的屏幕空间?
是的。当您运行以下程序时,它们会列在控制台窗口中:
#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; }在我的语言环境中,列表似乎只是Tomorrow 和昨天。
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?
解决方案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".
更多推荐
相对日期格式,过去日期的输出?
发布评论