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

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -