node ninja
node ninja

Reputation: 32986

How to display time from UIDatePicker

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];

Upvotes: 0

Views: 1571

Answers (2)

Anita
Anita

Reputation: 11

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"];

  }

Upvotes: 1

Sam
Sam

Reputation: 2707

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?

Upvotes: 1

Related Questions