1

first of all, I know this has been asked hundreds of times and I read probably 99 of all of the questions and answers. My issue is that I'm using times in my application and I have a setting, that lets the user override the actual system time-format. Means if the system uses 24h format, the user can still display the time in 12h format using that override setting.

However, it doesn't always work, here the cases:

1) System = 12h
Override off: displays 12h format
Override on (to 24h): displays 24h format 
Override on (to 12h): displays 12h format

2) System = 24h
Override off: displays 24h format
Override on (to 24h): displays 24h format 
Override on (to 12h): displays 24h format

It seems like in case 2) that I can't get it to display the 12h format :-/ And I've been trying all kinds off different things already. I'm fetching the setting (bool) and time from Core-Data. here is where I set my formatter in an if statement, which works that I verified using NSLog.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if (use24hFormat) {
     // we have to use 24h time format
     [formatter setDateFormat:@"HH:mm"];
     NSLog(@"We have to use 24h-Format);
 } else if (!use24hFormat) {
     // we have to use 12h time format
     [formatter setDateFormat:@"hh:mm a"];
     NSLog(@"We have to use 12h-Format);
 }

Any ideas?? Thanks guys

OUTPUT:

I believe you, I just ran your code (on iPhone with 24h format not simulator) and it does not work :-( Here is the OUTPUT

2014-06-27 21:02:39.565 Test[687:60b] We have to use 24h-Format
2014-06-27 21:02:39.572 Test[687:60b] timeString: 21:02
2014-06-27 21:02:39.574 Test[687:60b] We have to use 12h-Format
2014-06-27 21:02:39.576 Test[687:60b] timeString: 21:02

Then I ran it on simulator (also 24h format)
[3440:60b] We have to use 24h-Format
[3440:60b] timeString: 21:05
[3440:60b] We have to use 12h-Format
[3440:60b] timeString: 9:05 PM
Studiologe
  • 187
  • 8
  • Have you read http://waracle.net/iphone-nsdateformatter-date-formatting-table/? – jakenberg Jun 28 '14 at 01:22
  • And I'm already using lower case h's for the hours in 12h format... – Studiologe Jun 28 '14 at 01:26
  • @jakepeterson Yes, I saw the table, came across it before thanks for pointing it out tho. However I also tried upper-case K's and it resulted in the same. Strange to me is, that I only have that issues when system is 24h and override is set to 12h and NOT the other way around... Does it maybe relate to how the date is stored in Core-Data? I'm confuzzed – Studiologe Jun 28 '14 at 01:29
  • 1. Of course, the condition in your `else if` is redundant. 2. Just tried in the simulator, and I can get the format I want regardless of the system settings. What do you use to format the date? That isn't shown in your code. – jcaron Jun 28 '14 at 01:33
  • @Zaph: I don't see how your comment helps with my issue, sorry. And as you don't seem to have a solution for my issue I'd like to kindly ask you to only contribute/respond in useful and respective ways. I'm all ears if you have any bugs to report. BTW I'm using Xcode 5 (iOS SDK 7.1) Thanks – Studiologe Jun 28 '14 at 01:36
  • It is a good idea to learn there terminology of a domain if you want to communicate with others. If the problem is with a date formatter and the question says Xcode there is a mismatch. – zaph Jun 28 '14 at 01:48
  • You're right, I could have chosen a different title, it's confusing in that way. Anyhow @jcaron I'm running it on my phone, how is it redundant? I'm using the ! to negate the statement. I know, just plain else {} would have done it too, but for readability I did the !. – Studiologe Jun 28 '14 at 01:54
  • I think you're bumping into the [locale "feature"](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature). – Hot Licks Jun 28 '14 at 03:51

1 Answers1

0

Edited to provide examples using H and h:

For the date formatting codes see: Formatting Dates and Times

Here is my test that works as @jcaron also verified:

NSDate *date = [NSDate date]; // Where my test date is in the PM
BOOL use24hFormat;
NSString *timeString;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

use24hFormat = YES;
if (use24hFormat) {
    [formatter setDateFormat:@"H:mm"];
    NSLog(@"We have to use 24h-Format");
} else {
    [formatter setDateFormat:@"h:mm a"];
    NSLog(@"We have to use 12h-Format");
}
timeString = [formatter stringFromDate:date];
NSLog(@"timeString: %@", timeString);

use24hFormat = NO;
if (use24hFormat) {
    [formatter setDateFormat:@"H:mm"];
    NSLog(@"We have to use 24h-Format");
} else {
    [formatter setDateFormat:@"h:mm a"];
    NSLog(@"We have to use 12h-Format");
}
timeString = [formatter stringFromDate:date];
NSLog(@"timeString: %@", timeString);

NSLog output:

We have to use 24h-Format
timeString: 21:51
We have to use 12h-Format
timeString: 9:51 PM

If this does not work for the OP there is some other variable.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Not quite sure where in the world people count hours from 1 to 24 or 0 to 11. Most people I am aware of use 0 to 23 (24 hour format, from 0:00 to 23:59) or 1 to 12 (12 hour format, from 12:00 AM to 11:59 AM then 12:00 PM to 11:59 PM). So it would be h and H, not k and K. – jcaron Jun 28 '14 at 01:42
  • Thanks Zaph, I tried using kk and KK before, it didn't work. Neither does just single k/K. – Studiologe Jun 28 '14 at 01:44
  • I believe you, I just ran your code (on iPhone with 24h format not simulator) and it does not work :-( SEE updated question for OUTPUT – Studiologe Jun 28 '14 at 02:03
  • This is soooooo weird, when I run it on my iPhone (7.1 using 24h format) it doesn't work, if I run it in the simulator on my mac (osx 10.9 using 24h format) it flipping' works.... I'm going crazy – Studiologe Jun 28 '14 at 02:09
  • I ran it on my iPhone 5s (iOS 7.1.1) and got the correct results. For you it isn't working, I understand. Are you in a region that uses 24 hour time? What is your timezone? – zaph Jun 28 '14 at 02:11
  • And given the fact Zaph that you luckily use the actual date/time in your example and also a locally created bool and it still doesn't work on my phone tells me, that there is no problem with my core-data and how i store the date, as it has nothing to do with the date in your example. Thank you very much tho, for trying and messing with the code, appreciate it – Studiologe Jun 28 '14 at 02:11
  • my timezone is chicago (Central Daylight Time CST) – Studiologe Jun 28 '14 at 02:12
  • In my iPhone 4s settings it is also set to "Calendar: Gregorian" and "Region Format: United States" – Studiologe Jun 28 '14 at 02:14
  • So there should be no timezone issue. Note that if I set 24 hour time I get 24 hour strings for both. – zaph Jun 28 '14 at 02:15
  • did we just stumble over a bug here? Like mentioned before I'm using iOS 7.1 and yours is 7.1.1, but it's hard for me to believe that between those two releases no one filed a bug report for that.... if that is really wrong doing on Apples side and not ours – Studiologe Jun 28 '14 at 02:17
  • I'm sure I have seen this before on SO, search for 12 and 24 hour date formatting. Basically Apple want's the date and time displayed as appropriate for the user and the user's location. That can be a problem. – zaph Jun 28 '14 at 02:24