himanth himanth - 4 months ago 11
Objective-C Question

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

How do I set maximum date in

UIDatepickerView
as today 23:59pm.
I tried like this

datePicker.maximumDate = [NSDate date];


But it is not working.
How can I do this?

Answer

You can do something 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;