我想以“人性化的方式”显示NSDates,如“上周”或“前几天”.类似于
Pretty Time for Java的东西.
这样做最好的方式是什么,将NSDateFormatter进行子类化?在我走之前,重新开始轮子,这里有没有一些图书馆?
解决方法
这是Swift 2中的解决方案:
func formattedHumanReadable(date: NSDate) -> String {
let formatter = NSDateFormatter()
formatter.timeStyle = .NoStyle
formatter.dateStyle = .ShortStyle
formatter.doesRelativeDateFormatting = true
let locale = NSLocale.currentLocale()
formatter.locale = locale
return formatter.stringFromDate(date)
}