普通日期格式为"2007年6月13日"在斯威夫特 [英] Ordinary Date Format "13th June 2007" in Swift

查看:200
本文介绍了普通日期格式为"2007年6月13日"在斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,有人可以让我hoe个看起来像这样的日期格式: 2007年6月13日.请注意,日期中包含"th"一词.我想以我的格式添加它.谢谢!

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!

推荐答案

已针对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)

这篇关于普通日期格式为"2007年6月13日"在斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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