Mostrar AM / PM en mayúsculas en swift
Escribí el siguiente código para mostrar una fecha y hora en un formato particular:
let formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.LongStyle
formatter.timeStyle = .MediumStyle
formatter.dateFormat = "HH:mm a 'on' MMMM dd, yyyy"
let dateString = formatter.stringFromDate(newDate!)
println(dateString)
Salida
12:16 pm on July 17, 2015
Quiero mostrar 'pm' como 'PM'(en mayúsculas) y si el teléfono tiene formato de 24 horas, entonces AM/PM no debería aparecer. Ayúdame, por favor.
37
1 answers
Puede establecer su DateFormatter amSymbol y pmSymbol de la siguiente manera:
Xcode 8.3 * Swift 3.1
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "h:mm a 'on' MMMM dd, yyyy"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"
let dateString = formatter.string(from: Date())
print(dateString) // "4:44 PM on June 23, 2016\n"
100
Author: Leo Dabus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-04 19:16:52
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-04 19:16:52