I was working on an iPhone application, and I need to display the time in a label. So I wrote the follow:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm a"];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
timeLabel.text = formattedDateString;**
It displays time in label but in 24H format.
- Example: 15.00 PM instead of 3.00 PM
I want to display time in 12h format, i.e. 3.00 PM
What should I do?
Thanks in advance.
[dateFormatter setDateFormat:@"hh:mm a"];
Note the lower case h's.
Just take a look UNICODE LOCALE DATA MARKUP LANGUAGE
Guess you have to use KK instead of HH ...
Reading the documentation is a good practice.
HH- is just showing the 0 when its 2,3,4 oclock. It's NOT 0-12 AM/PM format.
See “Date Formatters” there for more details.
in your case:
Appendix F: Date Format Patterns
Swift 3.0