如何将带有字符串的自定义日期格式写入其中?
How can I write a custom Date Format with string into it?
愚蠢的问题:
我愿意在 Swift 中为巴西模式编写自定义日期格式,类似于:
"20 de Junho, 2017" // 20 of June, 2017
我正在尝试这种模式:
"dd de MMMM, yyyy"
但"de"
世界也被当作一种模式。我怎么能说这是一个常数?
谢谢
你必须用引号括起来'
,例如
"dd 'de' MMMM, yyyy"
Literal text, which is output as-is when formatting, and must closely match when parsing. Literal text can include:
- Any characters other than A..Z and a..z, including spaces and punctuation.
- Any text between single vertical quotes ('xxxx'), which may include A..Z and a..z as literal text.
您也可以让格式化程序生成 de
:
let locale = Locale(identifier: "pt-BR")
let dateFormatter = DateFormatter()
let dayFormat = DateFormatter.dateFormat(fromTemplate: "d MMMM", options: 0, locale: locale)!
dateFormatter.dateFormat = dayFormat + ", yyyy"
dateFormatter.locale = locale
print(dateFormatter.string(from: Date())) // 7 de junho, 2017
愚蠢的问题:
我愿意在 Swift 中为巴西模式编写自定义日期格式,类似于:
"20 de Junho, 2017" // 20 of June, 2017
我正在尝试这种模式:
"dd de MMMM, yyyy"
但"de"
世界也被当作一种模式。我怎么能说这是一个常数?
谢谢
你必须用引号括起来'
,例如
"dd 'de' MMMM, yyyy"
Literal text, which is output as-is when formatting, and must closely match when parsing. Literal text can include:
- Any characters other than A..Z and a..z, including spaces and punctuation.
- Any text between single vertical quotes ('xxxx'), which may include A..Z and a..z as literal text.
您也可以让格式化程序生成 de
:
let locale = Locale(identifier: "pt-BR")
let dateFormatter = DateFormatter()
let dayFormat = DateFormatter.dateFormat(fromTemplate: "d MMMM", options: 0, locale: locale)!
dateFormatter.dateFormat = dayFormat + ", yyyy"
dateFormatter.locale = locale
print(dateFormatter.string(from: Date())) // 7 de junho, 2017