ios - Convert NSString to NSDate in Spanish Locale -
i have integrate rss feed parser ios application. 1 of field in received data published date. i'm able parse date if iphone locale english-united states. when change language of iphone spanish, not able convert string nsdate object.
here's code wrote:
nsstring* dt = @"fri, 26 jun 2015 00:00:00"; nsdateformatter* dtformatter = [[nsdateformatter alloc] init]; //set locale spanish [dtformatter setlocale:[[nslocale alloc] initwithlocaleidentifier:@"es"]]; [dtformatter setdateformat:@"eee, dd mmm yyyy hh:mm:ss"]; nsdate* condt = [dtformatter datefromstring:dt ]; nslog(@"%@", condt); //this value (null)
even after setting locale "es" (which spanish), still not able parse properly. how can convert string date in ios?
when set locale, don't want use locale of device, rather locale used when string created (because you're taking english string , want convert nsdate
regardless of locale of device). in fact, it's advised use en_us_posix
:
[dtformatter setlocale:[nslocale localewithlocaleidentifier:@"en_us_posix"]];
see technical q&a #1480. focuses on gregorian calendar issue rfc 3999/iso 8601 date strings, applicable language settings, too.
by way, notice you're not setting timezone. when dates not bear timezone information, they've been converted gmt/utc/zulu. may want set timezone formatter, too:
[dtformatter settimezone:[nstimezone timezoneforsecondsfromgmt:0]];
given time component of string "00:00:00", perhaps isn't significant, if dealing datetime strings, want make sure correctly capture timezone used within string, well.
Comments
Post a Comment