Date Conversion

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

🙂

Answered by OOPer in 421001022

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)")
}
Accepted Answer

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)")
}

Thanks a ton 😍

Hi all! I'm starting with swift :D a question: if I'm creating a MLDateTable from a csv file, how do I convert a column from string to date?, and in another column, how do I convert the None values to 0 and double?

I tried the forms above but it tells me they don't support MLDatTable.

Date Conversion
 
 
Q