如何将 MYSQL 日期时间转换为 NSDate - Swift [英] How to Convert MYSQL DateTime to NSDate - Swift
本文介绍了如何将 MYSQL 日期时间转换为 NSDate - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
2015-06-30T17:30:36.000Z"是 MySQL 中 DateTime 值的示例.如何在 Swift 中将其转换为 NSDate?
"2015-06-30T17:30:36.000Z" is an example of DateTime value in MySQL. How to convert it to NSDate in Swift?
下面是我的代码,但它不起作用
Below is my code but it does not work
var sqlDateFormatter = NSDateFormatter()
sqlDateFormatter.dateFormat = "yyyy’-‘MM’-'dd’T'HH’:'mm’:'ss'Z’"
self.whenPublished = self.sqlDateFormatter.dateFromString("2015-06-30T17:30:36.000Z")
推荐答案
您只需要在 dateFormat 字符串的末尾添加它缺少的毫秒.试试这个:
You just need to add the milliseconds thats it is missing at the end of your dateFormat string. Try like this:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let string = "2015-06-30T17:30:36.000Z"
if let date = dateFormatter.date(from: string) {
print(date) // "2015-06-30 17:30:36 +0000"
}
注意:你需要使用普通的单齿条'
而不是弯曲的齿条'
Note: You need to use regular single cote '
instead of curved cote ’
这篇关于如何将 MYSQL 日期时间转换为 NSDate - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文