ios 在swift 3中将字符串转换为日期类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41435857/
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
Convert string to DATE type in swift 3
提问by Paolo Gdf
I have this structure:
我有这个结构:
struct message {
var id: String = "0"
var text: String = ""
var date: Date!
var status: String = ""
}
I have to load this structure from dbase, that it export in String
format also date
.
So I write this code to convert String
to Date
type:
我必须从 dbase 加载这个结构,它String
也以格式导出date
。所以我写了这段代码来转换String
为Date
类型:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!
let dataDate = dateFormatter.date(from: elemMessage["date"] as! String)!
And I load it in structure:
我在结构中加载它:
message(id: elemMessage["id"] as! String, text: elemMessage["text"] as! String, date: dataDate as! Date, status: elemMessage["status"] as! String)
But I have this warning: "Cast from Date
to unrelated type Date
always fails"
但我有这个警告:“从不Date
相关的类型转换Date
总是失败”
So if I run app it will fails.
因此,如果我运行应用程序,它将失败。
How Can I fix this, the date
var in structure have to be Date
type.
我该如何解决这个问题,date
结构中的var 必须是Date
类型。
Thank you.
谢谢你。
回答by Anand Nimje
You can convert String Dateinto Date/NSDatelike below code: -
您可以像下面的代码一样将字符串日期转换为日期/NSDate:-
Swift 3.2& Swift 4.2
斯威夫特 3.2和斯威夫特 4.2
String to Date
字符串到日期
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-mm-yyyy" //Your date format
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00") //Current time zone
//according to date format your date string
guard let date = dateFormatter.date(from: "01-01-2017") else {
fatalError()
}
print(date) //Convert String to Date
Date to String
日期到字符串
dateFormatter.dateFormat = "MMM d, yyyy" //Your New Date format as per requirement change it own
let newDate = dateFormatter.string(from: date) //pass Date here
print(newDate) //New formatted Date string
Output: -
输出: -
2017-01-11 00:07:00 +0000
Jan 11, 2017
回答by Rabie
Swift 4 ISO
斯威夫特 4 ISO
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone.autoupdatingCurrent
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
回答by Jeet Gandhi
func getDateFromString(dateStr: String) -> (date: Date?,conversion: Bool)
{
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let dateComponentArray = dateStr.components(separatedBy: "/")
if dateComponentArray.count == 3 {
var components = DateComponents()
components.year = Int(dateComponentArray[2])
components.month = Int(dateComponentArray[1])
components.day = Int(dateComponentArray[0])
components.timeZone = TimeZone(abbreviation: "GMT+0:00")
guard let date = calendar.date(from: components) else {
return (nil , false)
}
return (date,true)
} else {
return (nil,false)
}
}
//Input: "23/02/2017"
//输入:“23/02/2017”
//Output: (2017-02-23 18:30:00 +0000, true)
//输出:(2017-02-23 18:30:00 +0000,真)