ios Swift NSDateFormatter 未使用正确的语言环境和格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33145005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift NSDateFormatter not using correct locale and format
提问by Lord Vermillion
This is my code:
这是我的代码:
let currentDate = NSDate()
let usDateFormat = NSDateFormatter()
usDateFormat.dateFormat = NSDateFormatter.dateFormatFromTemplate("d MMMM y", options: 0, locale: NSLocale(localeIdentifier: "en-US"))
cmt.date = usDateFormat.stringFromDate(currentDate)
I was expecting to get "15 October 2015", but I got "oktober 15, 2015". The month is in swedish locale.
我期待得到“2015 年 10 月 15 日”,但我得到了“2015 年 10 月 15 日”。本月采用瑞典语。
What have I done wrong? Both locale and format are wrong.
我做错了什么?语言环境和格式都是错误的。
回答by ikbal
Try this:
尝试这个:
let dateString = "2015-10-15"
let formater = NSDateFormatter()
formater.dateFormat = "yyyy-MM-dd"
print(dateString)
formater.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let date = formater.dateFromString(dateString)
print(date)
Swift 3 Xcode 8
斯威夫特 3 Xcode 8
let dateString = "2015-10-15"
let formater = DateFormatter()
formater.dateFormat = "yyyy-MM-dd"
print(dateString)
formater.locale = Locale(identifier: "en_US_POSIX")
let date = formater.date(from: dateString)
print(date!)
I hope it helps.
我希望它有帮助。
回答by Zell B.
Check out the documentationof dateFormatFromTemplate
. It states that :
退房的文件的dateFormatFromTemplate
。它指出:
Return Value
A localized date format string representing the date format components given in template, arranged appropriately for the locale specified by locale.
The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.
返回值
一个本地化的日期格式字符串,表示模板中给定的日期格式组件,根据 locale 指定的区域设置适当排列。
返回的字符串可能不完全包含模板中给出的那些组件,但可能——例如——应用了特定于区域设置的调整。
So thats the problem about arranging and language. To get the date you are looking for you need to set date formatter's dateFormat
and locale
as follow:
这就是安排和语言的问题。要获得你需要设定的日期格式的您正在寻找的日期dateFormat
和locale
如下:
let currentDate = NSDate()
let usDateFormat = NSDateFormatter()
usDateFormat.dateFormat = "d MMMM y"
usDateFormat.locale = NSLocale(localeIdentifier: "en_US")
cmt.date = usDateFormat.stringFromDate(currentDate)
回答by mahdiTGK
try this code
试试这个代码
let locale1:Locale = NSLocale(localeIdentifier: "en_US") as Locale
var date = Date().description(with: locale1)
print(date)
//Monday, April 3, 2017...
//2017 年 4 月 3 日星期一...
回答by szubiszon
Better Swift 3/3.1 solution:
更好的 Swift 3/3.1 解决方案:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US")
回答by DàChún
You can use the Date extension in Swift5:
你可以在 Swift5 中使用 Date 扩展:
extension Date {
var timestamp: String {
let dataFormatter = DateFormatter()
dataFormatter.locale = Locale(identifier: "en_US_POSIX")
dataFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSS"
return String(format: "%@", dataFormatter.string(from: self))
}
}
You can get the timestamp by
您可以通过以下方式获取时间戳
print("Now: \(Date().timestamp)")