简体   繁体   English

Swift NSDateFormatter 未使用正确的语言环境和格式

[英]Swift NSDateFormatter not using correct locale and format

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".我期待得到“2015 年 10 月 15 日”,但我得到了“2015 年 10 月 15 日”。 The month is in swedish locale.本月采用瑞典语。

What have I done wrong?我做错了什么? Both locale and format are wrong.语言环境和格式都是错误的。

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.我希望它有帮助。

Check out the documentation of dateFormatFromTemplate .查看dateFormatFromTemplate文档 It states that :它指出:

Return Value返回值

A localized date format string representing the date format components given in template, arranged appropriately for the locale specified by locale.一个本地化的日期格式字符串,表示模板中给定的日期格式组件,根据 locale 指定的区域设置适当排列。

The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.返回的字符串可能不完全包含模板中给出的那些组件,但可能——例如——应用了特定于区域设置的调整。

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:要获取您要查找的日期,您需要按如下方式设置日期格式化程序的dateFormatlocale

let currentDate = NSDate()
let usDateFormat = NSDateFormatter()
usDateFormat.dateFormat = "d MMMM y"
usDateFormat.locale = NSLocale(localeIdentifier: "en_US")
cmt.date = usDateFormat.stringFromDate(currentDate)

Better Swift 3/3.1 solution:更好的 Swift 3/3.1 解决方案:

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US")

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 日星期一...

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)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM