-3

I searched for hours, but I only found samples like this:


let dateString = "2020-03-02T19:37:00.073+01:00" //get from api
let dateFormatter = ISO8601DateFormatter()

dateFormatter.formatOptions = [
    .withTimeZone,
    .withInternetDateTime,
    .withFractionalSeconds
]

print(dateFormatter.date(from: dateString) ?? "no valid date")

//2020-03-02 18:37:00 +0000

How can I formate this date like this:

02.03.2020 or 02.03.2020 18:37 (German format)

The method ISO8601DateFormatter() has no dateStyle property?

How can I display the formated date in a label?

Thanks

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Jack
  • 407
  • 2
  • 6
  • 15
  • The formats you want are not ISO8601 format. Use normal `DateFormatter` to get them. You are printing a `Date` to console. You have to convert that `Date` to a `String`. – Sulthan Mar 11 '20 at 18:49
  • Does this answer your question? [Date Format in Swift](https://stackoverflow.com/questions/35700281/date-format-in-swift) – Joakim Danielson Mar 11 '20 at 18:53
  • i want to change the given date (string: 2020-03-02T19:37:00.073+01:00) in to the German format. In the example above, only the current date is formatted. How can I do this, thx. – Jack Mar 11 '20 at 19:06
  • 1
    Sorry I misread, [this question](https://stackoverflow.com/questions/38503489/how-to-convert-date-format-from-dd-mm-yyyy-to-yyyy-mm-dd-in-swift) should help you – Joakim Danielson Mar 11 '20 at 20:36

2 Answers2

0

I have this reference for a POC of Salesforce, they uses this format: https://salesforce.stackexchange.com/questions/108896/cannot-deserialize-instance-of-datetime-from-value-string-value

And I have an extensions for use:

extension Date {
    func salesforceFormat() -> String {
        let dateformat = DateFormatter.salesforceDateFormatter
        return dateformat.string(from: self)
    }
}

extension String {
    func fromSalesforceDateToDate() -> Date? {
        let dateformat = DateFormatter.salesforceDateFormatter
        return dateformat.date(from: self)
    }
}

extension DateFormatter {
    static var salesforceDateFormatter: DateFormatter {
        let dateformat = DateFormatter()
        dateformat.calendar = Calendar(identifier: .iso8601)
        dateformat.locale = Locale(identifier: "en_US_POSIX")
        dateformat.timeZone = TimeZone(secondsFromGMT: 0)
        dateformat.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
        return dateformat
    }
}

Then you can convert from string to Date and from Date to String. Extensions are not the best solution but work for example.

Maetschl
  • 1,330
  • 14
  • 23
0

thx for your help

here my solution:

func convertDateFormater(_ date: String) -> String
{
    let dateFormatter = ISO8601DateFormatter()
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.formatOptions = [.withFractionalSeconds, .withTimeZone, .withInternetDateTime]
    let date = dateFormatter.date(from: date)

    let dateFormatter2 = DateFormatter()
    dateFormatter2.dateFormat = "dd.MM.yyyy"
    return  dateFormatter2.string(from: date!)
}

Jack
  • 407
  • 2
  • 6
  • 15