来自 NSDate 的相对字符串 [英] Relative string from NSDate

查看:35
本文介绍了来自 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

推荐答案

NSDateFormatter 将通过在实例上使用 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.

如果这不能满足您的需求,请查看 SORelativeDateTransformer,它是一个 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.

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

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 的相对字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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