ios - Weird behavior using NSDateformatter -
first of all, know has been asked hundreds of times , read 99 of of questions , answers. issue i'm using times in application , have setting, lets user override actual system time-format. means if system uses 24h format, user can still display time in 12h format using override setting.
however, doesn't work, here 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 in case 2) can't display 12h format :-/ , i've been trying kinds off different things already. i'm fetching setting (bool) , time core-data. here set formatter in if statement, works verified using nslog.
nsdateformatter *formatter = [[nsdateformatter alloc] init]; if (use24hformat) { // have use 24h time format [formatter setdateformat:@"hh:mm"]; nslog(@"we have use 24h-format); } else if (!use24hformat) { // have use 12h time format [formatter setdateformat:@"hh:mm a"]; nslog(@"we have use 12h-format); } any ideas?? guys
output:
i believe you, ran code (on iphone 24h format not simulator) , not work :-( here output 2014-06-27 21:02:39.565 test[687:60b] have 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] have use 12h-format 2014-06-27 21:02:39.576 test[687:60b] timestring: 21:02 ran on simulator (also 24h format) [3440:60b] have use 24h-format [3440:60b] timestring: 21:05 [3440:60b] have use 12h-format [3440:60b] timestring: 9:05 pm
edited provide examples using h , h:
for date formatting codes see: formatting dates , times
here test works @jcaron verified:
nsdate *date = [nsdate date]; // test date in pm bool use24hformat; nsstring *timestring; nsdateformatter *formatter = [[nsdateformatter alloc] init]; use24hformat = yes; if (use24hformat) { [formatter setdateformat:@"h:mm"]; nslog(@"we have use 24h-format"); } else { [formatter setdateformat:@"h:mm a"]; nslog(@"we have use 12h-format"); } timestring = [formatter stringfromdate:date]; nslog(@"timestring: %@", timestring); use24hformat = no; if (use24hformat) { [formatter setdateformat:@"h:mm"]; nslog(@"we have use 24h-format"); } else { [formatter setdateformat:@"h:mm a"]; nslog(@"we have use 12h-format"); } timestring = [formatter stringfromdate:date]; nslog(@"timestring: %@", timestring); nslog output:
we have use 24h-format
timestring: 21:51
have use 12h-format
timestring: 9:51 pm
if not work op there other variable.
Comments
Post a Comment