1

I am trying to localised string.

In english i am getting like "Friday, Jun 26"

but in spanish its like "jueves, jun 25".

first letter is small. but I am trying to get just as English with first letter caps.

Bellow is my code.

  let longDateFormatter = DateFormatter()
  longDateFormatter.dateFormat = "EEEE, MMM d"
  longDateFormatter.locale = Locale(identifier: "es")

is there any way to get date with first letter caps. Thanks for help/

  • 1
    Compare https://stackoverflow.com/a/59434614 – Martin R Jun 26 '20 at 14:35
  • Also possibly helpful: https://stackoverflow.com/q/26306326 – Martin R Jun 26 '20 at 14:39
  • Also, for proper localization consider using [.setLocalizedDateFormatFromTemplate](https://developer.apple.com/documentation/foundation/dateformatter/1417087-setlocalizeddateformatfromtempla) instead of direct `dateFormat` assignment. – user28434'mstep Jun 26 '20 at 14:39
  • 1
    Just for context (though you might already know this), weekdays aren't capitalized in Spanish like they are in English – Alexander Jun 26 '20 at 14:43

1 Answers1

1

Apparently Spanish does not capitalize the names of months and days of the week like we do in English. Thus the format you are getting is correct for Spanish, and you should stop trying to change it. (in essence, this is an x/y problem.)

See this link: https://www.spanishdict.com/answers/181002/do-months-need-to-be-capitalized-in-spanish#:~:text=Spanish%20does%20NOT%20capitalize%3A&text=%3F-,Calendar%3A%20Names%20of%20the%20days%20of%20the,and%20months%20of%20the%20year.&text=%3F-,Nationality%3A%20Although%20names%20of%20countries%20and%20cities%20are%20capitalized%2C%20words,derived%20from%20them%20are%20not.

If you want to do something different than the correct localization for Spanish you will need to take the output from the Spanish localization and manipulate it. You could simply use longDateFormatter.string(from: date).capitalized, which would capitalize every word in the resulting date string.

let longDateFormatter = DateFormatter()
longDateFormatter.dateFormat = "EEEE, MMM d"
longDateFormatter.locale = Locale(identifier: "es")

let output = longDateFormatter.string(from: Date()).capitalized
print(output)

Yields

Viernes, Jun 26

But again, that is the WRONG way to display dates in Spanish. It is every bit as wrong as displaying "friday, june 26" in English.

Duncan C
  • 128,072
  • 22
  • 173
  • 272