ios - How to set maximum date in uidatepickerview as today 23:59? -


how set maximum date in uidatepickerview today 23:59pm. tried this

        datepicker.maximumdate = [nsdate date]; 

but not working. how can this?

you can like,

  nsdateformatter *df = [[nsdateformatter alloc]init];  [df setdateformat:@"yyyy-mm-dd"];  nsdate *currentdate = [nsdate date];  nsstring *currentdatestr = [[df stringfromdate:currentdate] stringbyappendingstring:@" 23:59"];  [df setdateformat:@"yyyy-mm-dd hh:mm"];  nsdate *maxdate = [df datefromstring:currentdatestr];  nslog(@"max date : %@",maxdate);  nslog(@"max date str : %@",[df stringfromdate:maxdate]);  yourdatepicker.maximumdate = maxdate; 

update :

another approach using nsdatecomponents suggested @droppy,

  nsdatecomponents *datecomponents = [[nscalendar currentcalendar] components: nscalendarunitday | nscalendarunitmonth | nscalendarunityear fromdate:[nsdate date]]; nsinteger day = [datecomponents day]; nsinteger month = [datecomponents month]; nsinteger year = [datecomponents year]; datecomponents.hour = 23; datecomponents.minute = 59;  nslog(@"%ld : %ld : %ld",(long)day,(long)month,(long)year);  nscalendar *gregoriancalendar = [[nscalendar alloc] initwithcalendaridentifier:nscalendaridentifiergregorian]; nsdate *resultdate = [gregoriancalendar datefromcomponents:datecomponents];  nsdateformatter *df = [[nsdateformatter alloc]init]; [df setdateformat:@"yyyy-mm-dd hh:mm"];  nslog(@"result date : %@",resultdate); nslog(@"result date string : %@",[df stringfromdate:resultdate]);  yourdatepicker.maximumdate = resultdate; 

Comments