ios - NSDate conversion to NSString doesn't follow NSDateFormatter -


this question has answer here:

i have problems nsdate nsstring conversion, have following code in method

nsdateformatter *formatter = [[nsdateformatter alloc] init]; nstimezone *timezone = [nstimezone timezonewithname:@"utc"]; [formatter settimezone:timezone]; [formatter setdateformat:@"yyyy'-'mm'-'dd't'hh':'mm':'ss'.'zzz'"]; return [formatter stringfromdate:date]; 

if i´m not mistaken should return string

"2016-01-15t22:45:40.gmt" 

but resulting string depends on device configuration, meaning if device shows date 24 hour result above, on other hand if device configured show house a.m. p.m.

"2016-01-15t10:46:50 p.m..gmt" 

from documentation:

although in principle format string specifies fixed format, default nsdateformatter still takes user’s preferences (including locale setting) account. must consider following points when using format strings:

in ios, user can override default am/pm versus 24-hour time setting. may cause nsdateformatter rewrite format string set.

i think you'll have consider using own formatting method if want result have fixed format, regardless of user settings.

actually, from answer (as pointed out pbasdf), appears can circumvent setting locale:

nslocale* en_us_posix = [[nslocale alloc] initwithlocaleidentifier:@"en_us_posix"]; [formatter setlocale:en_us_posix]; 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -