簡體   English   中英

NSDateFormatter 一起使用 locale 和 dateFormat 是否有意義?

[英]Does it make sense to use locale and dateFormat together for NSDateFormatter?

那么有意義嗎? 我認為 dateFormat 本身准確地指定了輸出,不是嗎?

如果您需要解析的日期,你可以同時使用兩種:

let formater = DateFormatter()
let enDateString = "2019, 16 January"
formater.locale = Locale(identifier: "en_US")
formater.dateFormat = "yyyy, dd MMMM"

let enDate = formater.date(from: enDateString)
print(enDate) // display: Optional(2019-01-16 00:00:00 +0000)

let frDateString = "16 janvier 2019"
formater.locale = Locale(identifier: "fr_FR")
formater.dateFormat = "dd MMMM yyyy"

let frDate = formater.date(from: frDateString)
print(frDate) // display: Optional(2019-01-16 00:00:00 +0000)

在這里, MMMM在模式dateFormat允許你解析月份在有針對性的純文本locale

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM