相对日期格式,过去日期的输出? [英] Relative Date Formatting, output for past dates?

查看:260
本文介绍了相对日期格式,过去日期的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 - [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".

这篇关于相对日期格式,过去日期的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆