-1

I need to convert "2022-01-20T00:00:00.000Z" to "dd MMM yyy" format. I have tried doing it as suggested in a stackoverflow answer but it returns nil

func convertDate(date:String)-> String{
    let dateFormatterGet = DateFormatter()
    dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let dateFormatterPrint = DateFormatter()
    dateFormatterPrint.dateFormat = "dd MMM yyy"
    if let date = dateFormatterGet.date(from: String(date)) {
        let convertedDate = dateFormatterPrint.string(from: date)
        return convertedDate
    } else {
        print("There was an error decoding the string")
    }
    return "nil"
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Shreeya
  • 9
  • 1
  • 3

2 Answers2

0

The fractional seconds are missing in the date format string. It's yyyy-MM-dd'T'HH:mm:ss.SSSZ.

And you are not using the date parameter. To avoid confusion with the local variable date rename it.

And you don't need two date formatters and it's highly recommended to set the Locale to a fixed value

func convertDate(dateString: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

    if let date = dateFormatter.date(from: dateString) {
       dateFormatter.dateFormat = "dd MMM yyy"
       return dateFormatter.string(from: date)
    } else {
       print("There was an error decoding the string")
       return ""
    }
   
} 
vadian
  • 274,689
  • 30
  • 353
  • 361
  • You always forget to set the calendar. This would "fail" if the user device 's calendar is set to a different calendar like Buddhist. The year would be more than 500 years offset. Btw `dd MMM yyyy` – Leo Dabus Jan 21 '22 at 05:41
-1

got the answer

func convertDate(date:String)-> String{
    let dateFormatterGet = DateFormatter()
    dateFormatterGet.dateFormat =  "yyyy-MM-dd'T'HH:mm:ss.sssZ"
    let dateFormatterPrint = DateFormatter()
    dateFormatterPrint.dateFormat = "dd MMM yyy"
    if let date = dateFormatterGet.date(from: String(date)) {
       let convertedDate = dateFormatterPrint.string(from: date)
       return convertedDate
    } else {
       print("There was an error decoding the string")
    }
   return "nil"
}
 print(convertDate(date: "2022-01-20T00:00:00.000Z"))
Shreeya
  • 9
  • 1
  • 3
  • This has numerous issues. Do you want me to enumerate them? – Leo Dabus Jan 21 '22 at 05:39
  • yes that would be very helpful – Shreeya Jan 21 '22 at 05:48
  • 1) `s` is for seconds. What you need is `S` for fractional seconds – Leo Dabus Jan 21 '22 at 05:50
  • 2) When working with fixed date format you should always set the locale to `"en_US_POSIX"`. Please take some time and read the [docs](https://developer.apple.com/library/archive/qa/qa1480/_index.html). Most importantly **you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences.** – Leo Dabus Jan 21 '22 at 05:53
  • 3) You should always set the formatter' s calendar to iso8601. If you don't set a calendar this would "fail" if the user device 's calendar is set to a different calendar like Buddhist. The year would be more than 500 years offset. – Leo Dabus Jan 21 '22 at 05:54
  • 4) you should use `yyyy` instead of `yyy` – Leo Dabus Jan 21 '22 at 05:55
  • 5) Last but not least. This would create a new (two) date formatters every time you call this method. You should create a static date formatter for parsing your dates. Note that there is an `ISO8601DateFormatter` which is exactly for this purpose (encoding/decoding iso8601 date strings) – Leo Dabus Jan 21 '22 at 05:58
  • Btw take a look at this [post](https://stackoverflow.com/a/28016692/2303865) – Leo Dabus Jan 21 '22 at 06:03
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 21 '22 at 16:01