objective c - NSDatePicker with date in yy to yyyy format -
i have nsdatepicker
, enter 0023 , expect change 2023
. logic convert yy
yyyy
based on +-50 years.
but default behavior of nsdatepicker
changes 0023
etc.
what need show in yyyy
format nearest 50 years range.
is there way through interface builder or through codes.
your highly appreciable.
it not "change" 0023
0023
, leaves @ 0023
, correct correct behaviour. you'd need manually check , fix yourself. maybe (untested):
nscalendar *calendar = [nscalendar currentcalendar]; nsdatecomponents *components = [calendar components:nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit fromdate:mypossiblyincorrectdate ]; nsdate *correcteddate; if ([components year] < 100) { nsuinteger currentyearlastdigits = 13; // insert logic current year here. nsuinteger yearlastdigits = [components year]; if (yearlastdigits > currentyearlastdigits) { [components setyear:1900 + yearlastdigits]; } else { [components setyear:2000 + yearlastdigits]; } correcteddate = [calendar datefromcomponents:components]; } else { correcteddate = mypossiblyincorrectdate; }
depending on how exact need be, might want get/set more components. see nscalendar constants.
but better catch wrong year number before number interpreted year number since date might invalid otherwise.
Comments
Post a Comment