71

I wrote the following code to show a datetime in a particular 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 phone has 24 hours format then AM/PM should not appear. Please help me.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Amit Raj
  • 1,358
  • 3
  • 22
  • 47

2 Answers2

204

You can set your DateFormatter amSymbol and pmSymbol as follow:

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"
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 2
    Thanks for your response. It works for me. But when phone has 24 hours format then AM/PM should not appear. can we achieve this? – Amit Raj Jul 17 '15 at 06:44
  • 1
    @LeoDabus You can actually since iOS overwrites 12/24 formats set in formatter with system settings unless you set locale explicitly. One of the methods http://stackoverflow.com/a/41817997/669586 – Sulthan Apr 02 '17 at 07:20
  • Not working for in Xcode 9.1 let formatter = DateFormatter() formatter.timeZone = TimeZone.current formatter.dateFormat = "hh:mm a" formatter.amSymbol = "AM" formatter.pmSymbol = "PM" – Darshan Mothreja Jan 04 '18 at 07:36
  • 1
    @DarshanMothreja No need to set the timeZone to current. The date formatter's default timeZone it is already the current timeZone. But when using fixed date formats you should set its locale to `"en_US_POSIX"` – Leo Dabus Jan 04 '18 at 19:20
  • @LeoDabus Thank you for quick reply and I will try this and update you. – Darshan Mothreja Jan 05 '18 at 07:24
  • @LeoDabus Still the result is same unable to achieve this let formatter = DateFormatter() formatter.dateFormat = "hh:mm a" formatter.amSymbol = "AM" formatter.pmSymbol = "PM" formatter.locale = Locale(identifier: "en_US_POSIX") – Darshan Mothreja Jan 05 '18 at 10:35
  • 1
    it works for me here `"08:36 AM"`. Try creating a new playground file and test it there – Leo Dabus Jan 05 '18 at 10:37
  • @DarshanMothreja you should always set the locale before setting the other properties – Leo Dabus Jul 04 '20 at 23:53
  • Have a try, `dateFormatter.dateFormat = "hh:mm a"` gives me `"11:48 AM"` upper period by default, maybe things are changed. @LeoDabus – Zhou Haibo Apr 21 '22 at 03:48
  • 1
    @ChuckZHB yes not sure when they have changed. Anyway using this approach will make sure it will never change regardless of the locale settings – Leo Dabus Apr 21 '22 at 04:07
30

Added some formats in one place. Hope someone get help.

Xcode 12 - Swift 5.3

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
var dateFromStr = dateFormatter.date(from: "12:16:45")!

dateFormatter.dateFormat = "hh:mm:ss a 'on' MMMM dd, yyyy"
//Output: 12:16:45 PM on January 01, 2000

dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
//Output: Sat, 1 Jan 2000 12:16:45 +0600

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
//Output: 2000-01-01T12:16:45+0600

dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
//Output: Saturday, Jan 1, 2000

dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
//Output: 01-01-2000 12:16

dateFormatter.dateFormat = "MMM d, h:mm a"
//Output: Jan 1, 12:16 PM

dateFormatter.dateFormat = "HH:mm:ss.SSS"
//Output: 12:16:45.000

dateFormatter.dateFormat = "MMM d, yyyy"
//Output: Jan 1, 2000

dateFormatter.dateFormat = "MM/dd/yyyy"
//Output: 01/01/2000

dateFormatter.dateFormat = "hh:mm:ss a"
//Output: 12:16:45 PM

dateFormatter.dateFormat = "MMMM yyyy"
//Output: January 2000

dateFormatter.dateFormat = "dd.MM.yy"
//Output: 01.01.00

//Output: Customisable AP/PM symbols
dateFormatter.amSymbol = "am"
dateFormatter.pmSymbol = "Pm"
dateFormatter.dateFormat = "a"
//Output: Pm

// Usage
var timeFromDate = dateFormatter.string(from: dateFromStr)
print(timeFromDate)
Atikul Gazi
  • 1,157
  • 12
  • 18