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:

  1. your format string mm dd, yyyy hh:mm:ss a not match string. needs mmmm dd, yyyy hh:mm:ss a. use of mm numeric months. use mmm abbreviated month names , use mmmm full month names.
  2. 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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -