来自NSDate的相对字符串

来自NSDate的相对字符串

本文介绍了来自NSDate的相对字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道一个库或者会将NSDate转换成下面例子的字符串?

Does anyone know of a library or something that will convert an NSDate into strings like the examples below?

1 hour ago
yesterday
last Thursday
2 days ago
last month
6 months ago
last year


推荐答案

将通过在实例上使用setDoesRelativeDateFormatting:来执行大量上述操作。从那里,您可以使用日期样式和时间样式来控制格式,以根据您的需要进行微调。

NSDateFormatter will do a great deal of what is mentioned above by using setDoesRelativeDateFormatting: on an instance. From there, you can control the formatting using the date style and the time style to fine tune it to your needs.

如果这样不能满足您的需要,请查看,它是一个NSValueTransformer子类,尝试提供您在SO上的相对日期看到的相同行为。

If that doesn't get you what you need then check out SORelativeDateTransformer, it is an NSValueTransformer subclass that attempts to provide the same behavior you see with relative dates on SO.

最后,如果你想滚动自己的或做更多的研究,这里是(可能)关于主题的原始问题... 。

Finally, if you want to roll your own or do more research here is the (likely) original question on the topic... yes it is question 11.

这篇关于来自NSDate的相对字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:21