IOS: NSDateFormatter for region format Italy -
i have tested app regiorn format usa , date display correct. when region changed italy live contains null value.
my starter string date is: - "may 2, 2013 6:46:33 pm"
the result date correct is: - "02/05/2013 18:46:33"
here code:
nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; dateformatter setdateformat:@"mm dd, yyyy hh:mm:ss a"]; nsdate *datefromstring; datefromstring = [dateformatter datefromstring:datastr]; [dateformatter setdateformat:@"dd/mm/yyyy hh:mm:ss"]; dateformatter.locale = [[nslocale alloc] initwithlocaleidentifier:@"en_us_posix"]; nsstring *stringfromdate = [dateformatter stringfromdate:datefromstring];
if starter string may 2, 2013 6:46:33 pm
have 2 issues:
- your format string
mm dd, yyyy hh:mm:ss a
not match string. needsmmmm dd, yyyy hh:mm:ss a
. use ofmm
numeric months. usemmm
abbreviated month names , usemmmm
full month names. - your date string has month names in english. if device setup italy won't parse month names since expect months names in italian.
your code should be:
nsstring *datestr = @"may 2, 2013 6:46:33 pm"; nsdateformatter *inputdateformatter = [[nsdateformatter alloc] init]; [inputdateformatter setdateformat:@"mmmm dd, yyyy hh:mm:ss a"]; [inputdateformatter setlocale:[[nslocale alloc] initwithlocaleidentifier:@"en_us_posix"]]; nsdate *datefromstring = [inputdateformatter datefromstring:datastr]; nsdateformatter *outputdateformatter = [[nsdateformatter alloc] init]; [outputdateformatter setdateformat:@"dd/mm/yyyy hh:mm:ss"]; nsstring *stringfromdate = [outputdateformatter stringfromdate:datefromstring];
Comments
Post a Comment