nsdateformatter - Show AM/PM in capitals in swift -


i wrote following code show datetime in particular format:

let formatter = nsdateformatter() formatter.datestyle = nsdateformatterstyle.longstyle formatter.timestyle = .mediumstyle formatter.dateformat = "hh:mm 'on' mmmm dd, yyyy" let datestring = formatter.stringfromdate(newdate!) println(datestring) 

output

12:16 pm on july 17, 2015 

i want show 'pm' 'pm'(in capitals) , if phone has 24 hours format am/pm should not appear. please me.

you can set dateformatter amsymbol , pmsymbol follow:

xcode 8.3 • swift 3.1

let formatter = dateformatter() formatter.dateformat = "h:mm '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" 

edit: objective c code

nsdateformatter *df = [[nsdateformatter alloc] init]; df.timezone = [nstimezone systemtimezone]; df.amsymbol = @"am"; df.pmsymbol = @"pm"; df.dateformat = "h:mm 'on' mmmm dd, yyyy" nsstring *stringdate = [df stringfromdate:datefromstring]; 

Comments