【问题标题】:Ordinary Date Format "13th June 2007" in Swift [duplicate]Swift 中的普通日期格式“2007 年 6 月 13 日”[重复]
【发布时间】:2016-05-31 02:47:51
【问题描述】:

你好,任何人都可以指点我制作一个如下所示的日期格式: 2007 年 6 月 13 日。注意这一天有“th”这个词。我想以我的格式添加它。谢谢!

【问题讨论】:

  • 你没有用谷歌搜索过这个吗?
  • 松散相关(不是同一个问题):stackoverflow.com/questions/31686225/…
  • 您可能会注意到这不是一种常见的格式。虽然读取序数后缀 (th),但通常不会写入。标准格式为“2007 年 6 月 13 日”或“2007 年 6 月 13 日”。通常,您希望使用为您决定这一点的系统格式化模式。

标签: ios swift nsdate nsdateformatter


【解决方案1】:

为 Swift 3 更新

你可以做这样的事情是区域安全的。

let calendar = Calendar.current
let date = Date()
let dateComponents = calendar.component(.day, from: date)
let numberFormatter = NumberFormatter()

numberFormatter.numberStyle = .ordinal

let day = numberFormatter.string(from: dateComponents as NSNumber)
let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "MMM yyyy"

let dateString = "\(day!) \(dateFormatter.string(from: date))"

print(dateString)

【讨论】:

  • 不幸的是,有些语言在日期中不使用序数。例如,这适用于英语,但在西班牙语中会失败(.° 不用于日期)。
  • 一位哥伦比亚裔美国同事告诉我,西班牙语在本月的第一天使用序数而不是其他月份,因此“12 月 1 日”将被称为“primero de diciembre”,而“12 月 2 日”会说成“dos de diciembre”
猜你喜欢
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 2015-04-29
  • 1970-01-01
  • 2014-03-21
相关资源
最近更新 更多