Take the 2-minute tour ×

I'm using a UIDatePicker in time mode to get the time. I'm trying to display the time selected in a label. The following code doesn't work

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"h:mm a"];

label.text = [outputFormatter stringFromDate:self.datePicker.date];

[outputFormatter release];
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

That code looks fine to me. Did you double check that your label/datepicker are linked up correctly (either through IB or however you created them)? Also, in what way specifically does the code not work?

share|improve this answer
You were right about the link. I forgot to make the link in IB. The code works. –  node ninja Sep 22 '10 at 4:55
add comment

It seems you are just trying to set the time format in setDateFormat.

Call the below function as your selector method - Resolved.

- (void)changeDateInLabel:(id)sender{

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateStyle:NSDateFormatterMediumStyle];
    [df setDateStyle:NSDateFormatterShortStyle];
    [df setDateFormat:@"MM/dd/yyyy"];
    [df setTimeZone:@"h:mm a"];

share|improve this answer
add comment

Your Answer


