Search code examples
iosobjective-cnsdateformatteruidatepicker

How can i display the UIDatePicker with HH:MM:SS


How can i display the UIDatePicker with format HH:MM:SS as there is no mode for this in UIDatePicker.Currently it is showing the date and time.I get to know that ,I have to use NSDate format .But I don't know how to use it.

I need to do this As I am getting the value in a format HH:MM:SS. need to display it on the picker and then change to any value after that have to save it .

below is the code that I use to create the custom picker.

CGRect pickerFrame = CGRectMake(0,self.view.bounds.size.height-250,0,0);
UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
myPicker.backgroundColor = [UIColor lightTextColor];
myPicker.layer.cornerRadius = 10;
myPicker.layer.masksToBounds=YES;

How and where can I use the formatter so that it display the following format .

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];

Solution

  • There are following types of modes available in UIDatePicker:

    UIDatePickerModeTime,
    UIDatePickerModeDate,
    UIDatePickerModeDateAndTime,
    UIDatePickerModeCountDownTimer
    

    With the following you can show time:

    datePicker.datePickerMode = UIDatePickerModeTime;
    

    But both date and time can not be shown.

    What you can do is either subclass UIPickerView or in after selecting the Date, prompt user to select time using the above pickerMode.