加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:01:28 所属栏目:百科 来源:网络整理
导读:所以我的目的是推出如下所示的日期: Today,August 28Tomorrow,August 29Friday,August 30...etc 问题是,似乎我只能这么接近. 当我将DoSRelativeDateFormatting:YES和setDateStyle设置为Full,setTimeStyle设置为None时,会产生如下结果: TodayTomorrowFrida
所以我的目的是推出如下所示的日期:
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.第一个,您将显示相对值,第二个将显示实际的日期.对于非相对日期,您将只显示非相对值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读