Show AM/PM in capitals in swift
Asked Answered
I

3

73

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.

Isogamete answered 17/7, 2015 at 5:53 Comment(0)
C
208

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"
Conservative answered 17/7, 2015 at 5:59 Comment(10)
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?Isogamete
@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 https://mcmap.net/q/275391/-detect-if-time-format-is-in-12hr-or-24hr-formatStenosis
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"Halmahera
@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"Conservative
@LeoDabus Thank you for quick reply and I will try this and update you.Halmahera
@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")Halmahera
it works for me here "08:36 AM". Try creating a new playground file and test it thereConservative
@DarshanMothreja you should always set the locale before setting the other propertiesConservative
Have a try, dateFormatter.dateFormat = "hh:mm a" gives me "11:48 AM" upper period by default, maybe things are changed. @LeoDabusDesolate
@ChuckZHB yes not sure when they have changed. Anyway using this approach will make sure it will never change regardless of the locale settingsConservative
S
33

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)
Scrumptious answered 26/10, 2020 at 14:19 Comment(0)
T
0

XCode 15: Swift 5.X

    let time = "22:22"
    let formatter = DateFormatter()
    formatter.dateFormat = "HH:mm"
    formatter.timeZone = TimeZone.current
    let date = formatter.date(from: time)
    formatter.dateFormat = "H:mm"
    formatter.timeStyle = .short
    formatter.timeZone = TimeZone.current
    formatter.amSymbol = "am"
    formatter.pmSymbol = "pm"
    print(formatter.string(from: date ?? Date()))

Output:

  10:22pm
Tudor answered 30/4, 2024 at 10:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.