In Datepicker i have set Min and Max time where uidatepicker can scroll for example i have set 3:00PM as minimum time and 8:00PM as maximum time , when i run the app i want the datepicker to display the Minimum time not the current time of the device . I have hard coded UIDatepicker in my project havent used in xib file . I have set Min time but when i switch from control.tag ==1 to 2 the minimum time changes and also when i open the app for first time the datepicker doesnt start from minimum time.
UPDATE :
-(IBAction)notificationButtonChanged:(UIButton *)control
{
if (control.tag == 1)
{
self.selectedButton = control.tag;
[userTimePicker reloadInputViews];
[components setHour:7];;
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:11];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
}
if (control.tag == 2)
{
self.selectedButton = control.tag;
[userTimePicker reloadInputViews];
[components setHour:12];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:17];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
}
}
EDIT:
Pass String into method and format to date...
While change the Picker, it convert time to UTC+00:00, But it displays time to your time Zone eg. India, it add UTC+05:30 to the Time. If any TimeZone problem set TimeZone to the NSDateFormatter.
The UIDatePicker class has a
setDate:animated:
method that you can use to set the date to whatever date/time you want on the date picker instance.More info: http://developer.apple.com/library/ios/documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html#//apple_ref/doc/uid/TP40006806-CH3-SW1
For Swift version: