Show AM / PM in capitals quickly

I wrote the following code to show the date-time in a specific format:

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) 

Output

 12:16 pm on July 17, 2015 

I want to show "pm" as "PM" (in capitals), and if the phone has a 24-hour format, then AM / PM should not appear. Please help me.

+28
source share
1 answer

You can set your DateFormatter amSymbol and pmSymbol as follows:

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" 
+78
source

Source: https://habr.com/ru/post/1382034/


All Articles