微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.第一个,您将显示相对值,第二个将显示实际的日期.对于非相对日期,您将只显示非相对值.

原文地址:https://www.jb51.cc/c/112690.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐