iOS Swift 3:将“yyyy-MM-dd'T'HH:mm:ssZ”格式字符串转换为日期对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41907419/
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
iOS Swift 3 : Convert "yyyy-MM-dd'T'HH:mm:ssZ" format string to date object
提问by Abhishek Thapliyal
Hello i have a dictionary
你好,我有一本字典
self.publishedAt = dictionary["publishedAt"] as? NSString
in which i'm getting date "2017-01-27T18:36:36Z". I want to convert it in readable format : dd-MM-yyyy hh:mm:ss. i tried via
其中我得到日期“ 2017-01-27T18:36:36Z”。我想将其转换为可读格式:dd-MM-yyyy hh:mm:ss。我试过
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm:ss"
let date = dateFormatter.date(from: (self.publishedAt as? String)!)
print("EXACT_DATE : \(date)")
But getting nil. :(
但是变成零。:(
What is the correct way to get date in simple format?
以简单格式获取日期的正确方法是什么?
回答by vadian
You need an input format to convert the ISO8601 string to date and an output format to convert the date back to string:
您需要输入格式将 ISO8601 字符串转换为日期,并需要输出格式将日期转换回字符串:
let string = "2017-01-27T18:36:36Z"
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
回答by Rob
To convert string to Date
object:
将字符串转换为Date
对象:
let string = "2017-01-27T18:36:36Z"
let isoFormatter = ISO8601DateFormatter()
let date = isoFormatter.date(from: string)!
Or, if you need to support iOS versions that predate ISO8601DateFormatter
:
或者,如果您需要支持早于 的 iOS 版本ISO8601DateFormatter
:
let isoFormatter = DateFormatter()
isoFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssX"
isoFormatter.locale = Locale(identifier: "en_US_POSIX")
isoFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let date = isoFormatter.date(from: string)!
(To understand why we set the locale
for the ISO 8601 date formatter, see Apple Technical Q&A 1480.)
(要了解为什么我们locale
为 ISO 8601 日期格式化程序设置了,请参阅Apple 技术问答 1480。)
Then, to convert that to a user-friendly date format, you'd use a separate formatter (or use the second example, above, you can re-use the formatter, but remember to reset the locale
back to Locale.current
):
然后,要将其转换为用户友好的日期格式,您将使用单独的格式化程序(或使用上面的第二个示例,您可以重新使用格式化程序,但请记住将其重置locale
为Locale.current
):
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
let result = formatter.string(from:date)
Note, I'd suggest using those style parameters when presenting the date back to the user rather than the dateFormat
string, so it's using the styles appropriate for their locale, rather than assuming they want 24h clock or not and/or whether they use dd-MM-yyyy
vs MM-dd-yyyy
format.
请注意,我建议在向用户显示日期而不是dateFormat
字符串时使用这些样式参数,因此它使用适合其语言环境的样式,而不是假设他们想要 24 小时时钟和/或他们是否使用dd-MM-yyyy
vsMM-dd-yyyy
格式.
Note, changing the formats of date formatter (e.g. changing the dateFormat
string) is a relatively expensive process, so if you're performing this process for multiple dates, do not take a single DateFormatter
and constantly change its dateFormat
or styles repeatedly back and forth (or worse, instantiate new formatters for each date). Instead, create one formatter per date format style and re-use it grammar as much as possible.
请注意,更改日期格式化程序的格式(例如更改dateFormat
字符串)是一个相对昂贵的过程,因此如果您要为多个日期执行此过程,请不要使用单个DateFormatter
并不断dateFormat
来回反复更改其或样式(或更糟,为每个日期实例化新的格式化程序)。相反,为每种日期格式样式创建一个格式化程序,并尽可能多地重复使用它的语法。
回答by Nil Rathod
Just used the function in your code(swift 4.2).
刚刚在您的代码中使用了该函数(swift 4.2)。
public func convertDateFormatter(date: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"//this your string date format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
dateFormatter.locale = Locale(identifier: "your_loc_id")
let convertedDate = dateFormatter.date(from: date)
guard dateFormatter.date(from: date) != nil else {
assert(false, "no date from string")
return ""
}
dateFormatter.dateFormat = "HH:mm a"///this is what you want to convert format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
let timeStamp = dateFormatter.string(from: convertedDate!)
print(timeStamp)
return timeStamp
}
Thanks
谢谢