【发布时间】:2015-08-21 15:20:48
【问题描述】:
我正在尝试将NSDates 格式化为一种在适用时使用相对格式而在不适用时使用星期几的格式:“今天”、“明天”、“星期日”、“星期一”……
问题是,NSDateFormatter 的 doesRelativeFormatting 仅在使用 dateStyle 时有效,而在 dateFormat 时无效。 (基本上,我需要 dateFormat = "EEEE" 的功能来维持后天的所有日子。)
目前,我正在使用以下代码:
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = .NoStyle
dateFormatter.dateStyle = .FullStyle
dateFormatter.doesRelativeDateFormatting = true
let dateString = dateFormatter.stringFromDate(theDate)
return dateString.componentsSeparatedByString(" ")[0]
这恰好在我的特定语言环境中工作,NSDateFormatterStyle.FullStyle 输出类似“2015 年 8 月 23 日星期日”的内容,但显然这不是一个好的或通用的解决方案。
我找到的最接近的东西是 this question,但对于我的用例来说,它似乎不必要地复杂,如果可能的话,我想要更优雅的东西。
谢谢!
【问题讨论】:
标签: ios swift cocoa cocoa-touch