-2

I have a string Fri, 16 Aug 2019 07:04:12 +0000 and I want to convert it to german string representation 16.8.2019 07:04:12

How can I do that in Swift 4?

Thanks a lot

4
  • 2
    Sorry but SO it is not a code writing service. Please edit your question, post what you have tried and the issues you are facing.
    – Leo Dabus
    Commented Aug 21, 2019 at 12:44
  • 1
    Note that you should not choose the date format which will be shown to the user. It should (most of the time) be displayed considering the user device and locale settings
    – Leo Dabus
    Commented Aug 21, 2019 at 12:45
  • Your problem has two steps 1. parse the string to a Date, which can you find a lot of times on SO. 2. format Date using DateFormatter, setting a specific locale. Nothing else is needed.
    – Sulthan
    Commented Aug 21, 2019 at 13:08
  • Related stackoverflow.com/questions/47980254/…
    – Sulthan
    Commented Aug 21, 2019 at 13:51

1 Answer 1

0

Usually we use DateFormatter to parse and format dates. Try this code.

let inputDateString = "Fri, 16 Aug 2019 07:04:12 +0000"

let formatter = DateFormatter()
formatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
formatter.timeZone = TimeZone(secondsFromGMT: 0)

let inputDate = formatter.date(from: inputDateString)

let germanDateFormatter = DateFormatter()
germanDateFormatter.dateFormat = "d.M.yyyy HH:mm:ss"
germanDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

if let date = inputDate {

    let result = germanDateFormatter.string(from: date)

    print(result) //16.8.2019 07:04:12
}

Also you can use this service to find common date formats https://nsdateformatter.com

As suggested in comments you can use Locale to format the date according to the user Locale. But you will get a bit another format then you need.

let inputDateString = "Fri, 16 Aug 2019 07:04:12 +0000"

let formatter = DateFormatter()
formatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
formatter.timeZone = TimeZone(secondsFromGMT: 0)

let inputDate = formatter.date(from: inputDateString)

let germanDateFormatter = DateFormatter()
germanDateFormatter.locale = .init(identifier: "de")
germanDateFormatter.dateStyle = .short
germanDateFormatter.timeStyle = .medium
germanDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

if let date = inputDate {

    let result = germanDateFormatter.string(from: date)

    print(result) //16.08.19, 07:04:12
}
3
  • 5
    Never ever write formatting strings explicitly outside parsing and encoding. Just set locale to de and let the system format it for you, or generate the formatting string from a pattern. Setting time zone for the formatter in this case is probably also wrong and you have to set POSIX locale for the parser.
    – Sulthan
    Commented Aug 21, 2019 at 13:04
  • @Sulthan, I wrote the code that fits requirements. The question is how to convert one format to another. I'm not sure that German locale has exact requested format.
    – Dmitry
    Commented Aug 21, 2019 at 13:14
  • 1
    It does. That's the purpose of the question. Nevertheless, your parsing will fail on most devices unless you set parser locale to POSIX.
    – Sulthan
    Commented Aug 21, 2019 at 13:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.