Display time in 12 Hours format in Objective-C with a.m/p.m instead AM/PM

I am trying to format time in 12 hours mode, but using a.m./p.m.


If I use the following code:

NSDateFormatter *formataTempo = [[NSDateFormatter alloc] init];

[formataTempo setDateFormat:@"hh:mm:ss a"];

I get something like this:

10:59:56 AM

But what I want is:

10:59:56 a.m.


What format string I should use?


Thanks in advance,


Elga.

Kudos to you for wanting to use the more correct abbreviation. As a card carrying pedant that's something that's always bugged me about iOS.


I don't know if it's possible with NSDateFormatter. Hopefully someone who knows more will chime in. Personally I would probably "post process" the string so that if the date formatter were to magically start doing the Right Thing later, the code would still work.


myStr = [myStr stringByReplacingOccurrencesOfString:@"AM" withString:@"a.m."];  // and the same for p.m.

junkpile's answer is the best approach. You can insert a simple single quote text into the format and it will print out whatever you want - so you could add 'a.m.' but you need to determine whether to add 'a.m.' or 'p.m.'. junkpile's approach incorporates that need explicitly and simply.


P.S. I am appealing a parking ticket based on a sign that said '2 hour parking Mon 8 AM to Sun 12AM' that I got Sunday afternoon after being in the space for 3 hours. They say Sun 12AM == Sun MID and both occur late on Sunday night.

Display time in 12 Hours format in Objective-C with a.m/p.m instead AM/PM
 
 
Q