Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a custom Date Format with string into it?

silly question here:

I'm willing to write a custom date format in Swift for brazilian pattern, which will be something like:

"20 de Junho, 2017" // 20 of June, 2017

I was trying this pattern:

"dd de MMMM, yyyy"

But the "de" world is taken as a pattern as well. How can I say that this is a constant?

Thanks

like image 815
Rici Avatar asked Dec 02 '22 13:12

Rici


1 Answers

You have to wrap it into quotes ', e.g.

"dd 'de' MMMM, yyyy"

From Date Format Patterns:

Literal text, which is output as-is when formatting, and must closely match when parsing. Literal text can include:

  • Any characters other than A..Z and a..z, including spaces and punctuation.
  • Any text between single vertical quotes ('xxxx'), which may include A..Z and a..z as literal text.

You can also let the formatter generate the de:

let locale = Locale(identifier: "pt-BR")
let dateFormatter = DateFormatter()

let dayFormat = DateFormatter.dateFormat(fromTemplate: "d MMMM", options: 0, locale: locale)!

dateFormatter.dateFormat = dayFormat + ", yyyy"
dateFormatter.locale = locale

print(dateFormatter.string(from: Date())) // 7 de junho, 2017
like image 171
Sulthan Avatar answered Dec 08 '22 00:12

Sulthan