i'm trying turn given hour am/pm or 24-hour display depending on user's preferences.
i have:
+ (nsstring *)formatwithtemplate:(nsstring *)template { return [nsdateformatter dateformatfromtemplate:template options:0 locale:[nslocale currentlocale]]; } + (nsstring *)format:(nsdate *)date { [formatter setdateformat:[myformatter formatwithtemplate:@"h a"]]; [dateformatter setdatestyle:nsdateformattershortstyle]; [dateformatter settimestyle:nsdateformatternostyle]; return [formatter stringfromdate:date]; } example usage:
[myformatter format:24] display "24:00" if using 24 hour clock "12:00 pm" if using 12 hour clock (in user's device settings).
however, 12 hour clock keeps displaying: "12:00 pm". there way rid of :00 while respecting locale , user's settings displays: "12 pm" instead of "12:00 pm"?
when use:
[dateformatter setdatestyle:nsdateformattershortstyle]; [dateformatter settimestyle:nsdateformatternostyle]; it respects user's settings ignores format. however, if use format, ignores user's settings.
any ideas?
i let user settings prevail. you've got method string that. modify string necessary.
since you're confident need find , replace string ':00' that. here pretty simple:
nsstring *string = @"12:00 pm"; //get `dateformatter` `format:date` method have nsstring *newstring = [string stringbyreplacingoccurrencesofstring:@":00" withstring:@""]; nslog(@"newstring %@",newstring);
Comments
Post a Comment