ios - NSDateFormatter local date format issue -
i have nsdate, , need convert string.
usually use code:
nsdateformatter *formatter = [[nsdateformatter alloc] init]; [formatter setdateformat:@"mm/dd/yyyy"]; nsstring *stringfromdate = [formatter stringfromdate:date]; return stringfromdate;
but need show data according local date format. instance, in usa date starts month, in russia date starts day (@"dd/mm/yyyy"
). , may there different way of separators in different countries: '/', '.' ',' '-'
what best approach this?
update (according jon skeet answer):
if specify nsdateformattermediumstyle date "4.9.2013", me it's better "04.09.2013". nsdateformattershortstyle doesn't neither. there way add zeroes ?
see date formatter guide gives advice this, suggesting set style rather calling setdateformat
:
nsdateformatter
makes easy format date using settings user configured in international preferences panel in system preferences.nsdateformatter
style constants—nsdateformatternostyle
,nsdateformattershortstyle
,nsdateformattermediumstyle
,nsdateformatterlongstyle
, ,nsdateformatterfullstyle
—specify sets of attributes determine how date displayed according user’s preferences.
it gives listing showing example starting this:
nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdatestyle:nsdateformattermediumstyle]; [dateformatter settimestyle:nsdateformatternostyle];
it sounds you'd want nsdateformattershortstyle
instead though.
setdateformat
use custom format. more advice same document:
there broadly speaking 2 situations in need use custom formats:
- for fixed format strings, internet dates.
- for user-visible elements don’t match of existing styles
Comments
Post a Comment