3

I want to get a time string like "10:00 PM", and I use the NSDateFormatterShortStyle to achieve this. However when the system is set to use 24-hour format, what I got is "22:00".

Besides, I want to get a localized time string. For example, in Chinese, I want to get "下午10:00" instead of "10:00 PM". So I have to use the [NSLocale currentLocale], and can't use the en_US_POSIX locale.

Please help me to get a localized 12-hour format time string.

I need not only the hour part, and also the am/pm part. Besides, am/pm part may locate before the hour/minute part or after the hour/minute part depending on locale. Just like the system display time. When system is set to use 12-hour format, it's just simple. But when the system is using 24-hour format, I just can't get the localized 12-hour format time string.

Swordsfrog
  • 119
  • 2
  • 3
  • 8

3 Answers3

14

Well this should work:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"hh:mm a" options:0 locale:[NSLocale currentLocale]]];
NSString *theTime = [dateFormatter stringFromDate:[NSDate date]];

This should get you a date formatter that will give the date in 12 hour format. The lower case hh indicates hours 1-12 format. See the Unicode Technical Standard #35 section of Date Format Patterns for details on the format string.

Unfortunately iOS will rewrite the format string based on the user's 24-hour format preference setting. I can find no way around that setting using Cocoa's date formatting. Since the 24-hour format will drop the am/pm designation, parsing the resulting string to convert hours greater than 12 will result in ambiguous time strings. Therefore the only remaining options I can see is honoring the users 24-hour preference setting or using other localization code. You can write your own, or maybe find an open source replacement.

Mr. Berna
  • 10,525
  • 1
  • 39
  • 42
  • I tried it, but it doesn't work. dateFormatFromTemplate:option:locale: returns a time format string, not a NSDateFormatter object. – Swordsfrog Jun 08 '11 at 00:54
  • 1
    My original answer was wrong, so I heavily edited the answer. – Mr. Berna Jun 08 '11 at 16:38
  • Its worked for me thanks.. It gives me "11:00 PM" format.. I want like 11pm... so I tried with hha but it still gives 11 PM.. how to get it like "11pm" ? – Kiran S Aug 15 '12 at 10:58
  • 1
    @iphonegeek You can post process the string using NSString methods like lowercaseString and stringByReplacingOccurrencesOfString:withString:. – Mr. Berna Aug 15 '12 at 13:15
  • @Mr.Berna: Ok. Thanks. I thought there might be way to do it in dateformatter itself.. anyways thanks for your quick reply. I think this will work for me. – Kiran S Aug 15 '12 at 13:43
  • When I format it with "hh:mm" I get 01:35. Is there anyway to get 1:35? – Chase Roberts Feb 02 '13 at 05:58
  • This is still the case as of iOS 9. From what I can tell, every locale has a default hour format (en_US is 12-hour). If the user changes the switch in settings so the format doesn't match the default (use 24-hour), the app will always show that new setting unless you manually change the locale on the date formatter. – Craig Siemens Feb 09 '16 at 21:21
0

Swift 3.0

    var dateFormatter = DateFormatter()
    dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "hh:mm a", options: 0, locale: NSLocale.current)
    var theTime: String = dateFormatter.string(from: Date())
Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31
-3
NSInteger originalHour = hour;
BOOL isPm = YES;
if (hour >= 12) {
    if (hour > 12)
        hour -= 12;
}
else {
    isPm = NO;
}

if (hour == 0)
    hour = 12;
Jonny
  • 15,955
  • 18
  • 111
  • 232