objective-c – NSDateFormatter相对日期格式与自定义格式

所以我的目的是推出如下所示的日期:
Today,August 28
Tomorrow,August 29
Friday,August 30
...etc

问题是,似乎我只能这么接近.

当我将DoSRelativeDateFormatting:YES和setDateStyle设置为Full,setTimeStyle设置为None时,会产生如下结果:

Today
Tomorrow
Friday,August 30,2013

这产生了一年,并没有产生今天和明天的月份和日期.

我试过的其他代码是:

[NSDateFormatter dateFormatFromTemplate:@"eeeedMMM" options:0
                                                     locale:[NSLocale currentLocale]];

并产生以下结果:

Wednesday,August 28
Thursday,August 30

似乎,dateStyle和timeStyle,或相对日期格式覆盖自定义格式.

有没有办法完成这个日期格式,而不进入自定义实现?

提前致谢!

解决方法

根据关于NSDateFormatter的文档,我认为您的问题的唯一解决方案是提供两个日期字符串为您希望相对显示的日子.你应该记住,在不同的语言中,相对日期有差异.从文档:

If a date formatter uses relative date formatting,where possible it replaces the date component of its output with a phrase—such as “today” or “tomorrow”—that indicates a relative date. The available phrases depend on the locale for the date formatter; whereas,for dates in the future,English may only allow “tomorrow,” french may allow “the day after the day after tomorrow”.

我相信,如果您定义要比较例如昨天,今天和明天,您可以使用2个NSDateFormatter.第一个,您将显示相对值,第二个将显示实际的日期.对于非相对日期,您将只显示非相对值.

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...