Hi i'm new to swift just started learning
I want to convert string to date format then convert the date format to a string to pass it down to a label, was trying out several steps.
Steps Performed
let date = "2020-07-13T18:51:53.928+0000"
let formatter = DateFormatter()
let date2 = formatter.date(from: date)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let dateString = formatter.string(from: date2)
print("The Modified Date \(dateString)")
Result:
The Modified Date nil
Thanks
🙂
I'm not sure what you want to do, but your code does not compile.
Try something like this:
let date = "2020-07-13T18:51:53.928+0000"
let formatter1 = DateFormatter()
formatter1.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
formatter1.locale = Locale(identifier: "en_US_POSIX")
if let date2 = formatter1.date(from: date) {
let formatter2 = DateFormatter()
formatter2.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
formatter2.locale = Locale(identifier: "en_US_POSIX")
let dateString = formatter2.string(from: date2)
print("The Modified Date \(dateString)")
}