ios - NSDateFormatter results different from apple.cn screenshots -
ios - NSDateFormatter results different from apple.cn screenshots -
the iphones @ http://apple.com/iphone have 9:41 am
in status bar esoteric reasons. i'm trying create localized screenshots app, consulted http://apple.com/cn/iphone, utilize 上午9:41
consistently. however, when set locale zh-hans
or zh-hant
using -applelanguages
flag, nsdateformatter
spits out 9:41 上午
. here code i’m using:
nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; nsstring *originallocale = dateformatter.locale; dateformatter.locale = [nslocale localewithlocaleidentifier:@"en_us"]; [dateformatter setdateformat:@"h:mm a"]; nsdate *date = [dateformatter datefromstring:@"9:41 am"]; dateformatter.locale = originallocale; nsstring *datestring = [dateformatter stringfromdate:date];
this code produces right format in en_us
, wrong 1 in zh-hant
. there way localized version of @"h:mm a"
, other hardcoding each language?
don't set dateformat
, set timestyle
access appropriate built-in format. nsdateformattermediumstyle
looks right me. there's datestyle
should want it.
so, sample code:
nsdateformatter *newformatter = [nsdateformatter new]; newformatter.locale = [nslocale localewithlocaleidentifier:@"zh-hant"]; newformatter.timestyle = nsdateformattermediumstyle; nslog(@"%@", [newformatter stringfromdate:[nsdate date]]);
output:
上午12:10:40
ios objective-c localization nsdateformatter
Comments
Post a Comment