Ordinary Date Format “13th June 2007” in Swift [duplicate]

拜拜、爱过 提交于 2019-12-23 10:26:15

问题


hello can any one point me to hoe make a date format that looks like this: 13th June 2007. Notice the day has the word "th". I want to add that in my format. Thanks!


回答1:


Updated for Swift 3

You can do something like this which is locale safe.

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)


来源:https://stackoverflow.com/questions/37536082/ordinary-date-format-13th-june-2007-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!