-2

Im working with a API, when I ask for a date, this API get my a String like this:

20190717-0300

I want to show that date in a UILabel with this format: "dd '\(preposition)' MMMM"

My attempt was to make a string extension:

extension String {
 var toDate: String? {
        let dateFormatter = DateFormatter()
        let preposition = NSLocalizedString("of", comment: "Preposition of dates formatted")
        dateFormatter.dateFormat = "dd '\(preposition)' MMMM"
        dateFormatter.locale = Locale.current

        if let date = dateFormatter.date(from: self) {
            let dateString = dateFormatter.string(from: date)
            return dateString
        }

        return nil
    }
}

And then use it:

myLabel.text = thatString.toDate

But .toDate always return nil

Note: The answers I found on the site are cases with ISO format strings.

Expected result:

17 of July

Rey Bruno
  • 355
  • 1
  • 13
  • 1
    You need two date formatters when translating a date string from one format to another. – rmaddy Jul 29 '19 at 18:08
  • 1
    As a side note, you should avoid your hardcoded "dd prep MMMM" format. Use a properly localized format to avoid confusing users with an unnatural format. `dateFormatter.setLocalizedDateFormatFromTemplate("ddMMMM")`. This gives "MMMM dd" in the US, "dd 'de' MMMM" in Spain, and "dd, MMMM" in Germany, for example. – rmaddy Jul 29 '19 at 18:12
  • Thanks for your comments, I will try to implement them in my code. – Rey Bruno Jul 29 '19 at 18:14
  • Possible duplicate of [How can I convert string date to NSDate?](https://stackoverflow.com/questions/24777496/how-can-i-convert-string-date-to-nsdate) – Fogmeister Jul 29 '19 at 18:17

2 Answers2

1

Basically you need an input date format and an output date format.

extension String {
    var toDate: String? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyyMMddZ"

        if let date = dateFormatter.date(from: self) {
            let preposition = NSLocalizedString("of", comment: "Preposition of dates formatted")
            dateFormatter.dateFormat = "dd '\(preposition)' MMMM"
            let dateString = dateFormatter.string(from: date)
            return dateString
        }

        return nil
    }
}

I totally agree with rmaddy's comment to use setLocalizedDateFormatFromTemplate

The source of the date field symbols is unicode.org: Date Format Patterns

vadian
  • 274,689
  • 30
  • 353
  • 361
0

Turn this into extension if you like:

// function
func formatDate(_ from:String, preposition:String) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyyMMddZ"
    guard let date = dateFormatter.date(from: from) else {
        return nil
    }
    dateFormatter.dateFormat = "dd '\(preposition)' MMMM"
    return dateFormatter.string(from: date)
}

// example usage
let str = "20190717-0300"
if let formatted = formatDate(str, preposition: "of") {
    print(formatted)
}
Chris
  • 1,539
  • 13
  • 25
  • 1. How is this an extension? 2. How does this improve on the existing answer? 3. Don't post code-only answers. Explain what was wrong in the question and how this answer fixes the problem. – rmaddy Jul 29 '19 at 19:16
  • previous answer is fine. this one just removes unnecessary apostrophies in the formatter string and uses guard-let instead of simple if-let. Also note no longer uses "NSanything" since those will long term deprecate. Just helping someone out in case this , not here to score points. – Chris Aug 05 '19 at 18:32
  • `NSLocalizeedString` is still used for localized strings. And you need the `'` around `\(preposition)`. They are necessary to quote the string literal in the date format. – rmaddy Aug 05 '19 at 18:37
  • maddy preposition is a variable, it doesn't need quotes. – Chris Aug 05 '19 at 18:51
  • The date format needs the quotes around the result. For example, `dateFormat` will be equal to `"dd of MMMM"` in your code when it needs to be `"dd 'of' MMMM"`. – rmaddy Aug 05 '19 at 18:53
  • Maddy in the question he asked for expected output to not have quotes. It's bolded aka "17 of July" not "17 'of' July" – Chris Aug 06 '19 at 23:16
  • The quotes around the word in the date format are required. They won't appear in the final string. The quotes tell the data formatter that the `"o"` and the `"f"` are to be treated as literal and not format specifiers. – rmaddy Aug 06 '19 at 23:53