0

I have a timestamp string that takes the form:

2018-03-06T06:28:39.887Z

and need to format it into something more human readable, I have tried the below date format however the date doesn't parse

 let dateFormatter = DateFormatter()
 dateFormatter.dateFormat = "y-MM-ddTk:mm:ss.SSSZ"
 let date = dateFormatter.date(from: "2018-03-06T06:28:39.887Z")

What am I missing with my date format?

DevWithZachary
  • 3,545
  • 11
  • 49
  • 101
  • Your date format it is completely wrong. check this https://stackoverflow.com/questions/28016578/swift-how-to-create-a-date-time-stamp-and-format-as-iso-8601-rfc-3339-utc-tim/28016692#28016692 – Leo Dabus May 01 '18 at 12:44
  • Just parse your date string using the answer above then you can use https://stackoverflow.com/a/28347285/2303865 to display it localized to the user – Leo Dabus May 01 '18 at 12:45

1 Answers1

-2

Solution for Swift 3/4:

let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"

let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"

if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26"){
    print(dateFormatterPrint.string(from: date))
}
else {
   print("There was an error decoding the string")
}
Mike Koene
  • 330
  • 3
  • 15