无法将日期格式转换为 Swift 中的显示日期
Can't Convert Date Format Into Display Date In Swift
我一直在尝试将以“Thu Nov 12 2020 07:00:00 GMT+0000(格林威治标准时间)”格式保存的日期转换为“MMM d, yyyy”格式",但是我没有成功。
import Foundation
extension String {
// Example date read in from database.
// "Thu Nov 12 2020 07:00:00 GMT+0000 (Greenwich Mean Time)"
func convertToDate() -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
dateFormatter.locale = Locale(identifier: "en_US_POSSIX")
dateFormatter.timeZone = .current
return dateFormatter.date(from: self)
}
func convertToDisplayFormat() -> String {
guard let date = self.convertToDate() else { return "N/A"}
return date.convertToMonthDayTimeFormat()
}
}
import Foundation
extension Date {
func convertToMonthDayTimeFormat() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
print(dateFormatter.string(from: self))
return dateFormatter.string(from: self)
}
}
然后在我读入数据的文件中代码是
cell.appointmentTime?.text = "\(tableData.date!.convertToDisplayFormat())"
在设置 dateFormat 之前,您应该始终将 dateFormat 区域设置设置为 "en_US_POSIX"
。顺便说一句,您从数据库中读取的日期字符串看起来总是采用 UTC 格式,如果是这种情况,您应该将 dateFormatter 时区设置为零 secondsFromGMT 并转义日期字符串时区:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSSIX")
dateFormatter.dateFormat = "EEE MMM dd yyyy HH:mm:ss 'GMT+0000 (Greenwich Mean Time)'"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.date(from: "Thu Nov 12 2020 07:00:00 GMT+0000 (Greenwich Mean Time)")
您还应该创建一个静态日期格式化程序,以避免每次调用此方法时都创建一个新日期格式化程序:
extension Formatter {
static let customDate: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSSIX")
dateFormatter.dateFormat = "EEE MMM dd yyyy HH:mm:ss 'GMT+0000 (Greenwich Mean Time)'"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return dateFormatter
}()
static let yyyyMMMd: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("yyyyMMMd")
return dateFormatter
}()
}
extension String {
func convertToDate() -> Date? {
Formatter.customDate.date(from: self)
}
func convertToDisplayFormat() -> String {
convertToDate()?.convertToMonthDayTimeFormat() ?? "N/A"
}
}
extension Date {
func convertToMonthDayTimeFormat() -> String {
Formatter.yyyyMMMd.string(from: self)
}
}
游乐场测试:
"Thu Nov 12 2020 07:00:00 GMT+0000 (Greenwich Mean Time)".convertToDisplayFormat() // "Nov 12, 2020"
我一直在尝试将以“Thu Nov 12 2020 07:00:00 GMT+0000(格林威治标准时间)”格式保存的日期转换为“MMM d, yyyy”格式",但是我没有成功。
import Foundation
extension String {
// Example date read in from database.
// "Thu Nov 12 2020 07:00:00 GMT+0000 (Greenwich Mean Time)"
func convertToDate() -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
dateFormatter.locale = Locale(identifier: "en_US_POSSIX")
dateFormatter.timeZone = .current
return dateFormatter.date(from: self)
}
func convertToDisplayFormat() -> String {
guard let date = self.convertToDate() else { return "N/A"}
return date.convertToMonthDayTimeFormat()
}
}
import Foundation
extension Date {
func convertToMonthDayTimeFormat() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
print(dateFormatter.string(from: self))
return dateFormatter.string(from: self)
}
}
然后在我读入数据的文件中代码是
cell.appointmentTime?.text = "\(tableData.date!.convertToDisplayFormat())"
在设置 dateFormat 之前,您应该始终将 dateFormat 区域设置设置为 "en_US_POSIX"
。顺便说一句,您从数据库中读取的日期字符串看起来总是采用 UTC 格式,如果是这种情况,您应该将 dateFormatter 时区设置为零 secondsFromGMT 并转义日期字符串时区:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSSIX")
dateFormatter.dateFormat = "EEE MMM dd yyyy HH:mm:ss 'GMT+0000 (Greenwich Mean Time)'"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.date(from: "Thu Nov 12 2020 07:00:00 GMT+0000 (Greenwich Mean Time)")
您还应该创建一个静态日期格式化程序,以避免每次调用此方法时都创建一个新日期格式化程序:
extension Formatter {
static let customDate: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSSIX")
dateFormatter.dateFormat = "EEE MMM dd yyyy HH:mm:ss 'GMT+0000 (Greenwich Mean Time)'"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return dateFormatter
}()
static let yyyyMMMd: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("yyyyMMMd")
return dateFormatter
}()
}
extension String {
func convertToDate() -> Date? {
Formatter.customDate.date(from: self)
}
func convertToDisplayFormat() -> String {
convertToDate()?.convertToMonthDayTimeFormat() ?? "N/A"
}
}
extension Date {
func convertToMonthDayTimeFormat() -> String {
Formatter.yyyyMMMd.string(from: self)
}
}
游乐场测试:
"Thu Nov 12 2020 07:00:00 GMT+0000 (Greenwich Mean Time)".convertToDisplayFormat() // "Nov 12, 2020"