how we can get the time from UIDatePicker

Discussion in 'iOS Programming' started by rajneesh.deora, Sep 18, 2008.

  1. rajneesh.deora macrumors newbie

    Aug 19, 2008
    Hi All,
    I am using UIDatePicker having datePickerMode UIDatePickerModeTime.
    Now my probelm is that how we can set the label text as selected time from the UIDatePicker.

    I have used NSDateFormatter and set it as
    but it is for getting the date from UIDatePicker having mode UIDatePickerModeDate.
    Please help me to solve it out.
    waiting for your help.

    Thanks a lot...
  2. Fontano macrumors member

    Jun 27, 2008

    [yourlable setText:[ descriptionWithCalendarFormat:@"%m/%d/%Y %I:%M %p" timeZone:nil locale:nil]];
    This will set your text to the format: MM/DD/YYYY 12:00 pm
    So if you just want time: Change the format to: "%I:%M %p"

    See the documentation for all the options to format the string of your date
  3. vikysaran macrumors newbie

    Sep 25, 2009
    Use setDateStyle and setTimeStyle.
    i did this and it worked great-

    	CGRect rect = CGRectMake(0.0, 43.0,300, 20);
    	myPickerView = [[UIDatePicker alloc] initWithFrame:rect];
    	myPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    	myPickerView.datePickerMode = UIDatePickerModeDateAndTime;
    	[myPickerView addTarget:self action:@selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
    	[self.view addSubview:myPickerView];
    - (void)changeDateInLabel:(id)sender{
    	//Use NSDateFormatter to write out the date in a friendly format
    	NSDateFormatter *df = [[NSDateFormatter alloc] init];
    	[df setDateStyle:NSDateFormatterMediumStyle];
    	[df setTimeStyle:NSDateFormatterShortStyle];
    	NSString *dateNTime = [NSString stringWithFormat:@"%@", [df]];
    	[df release];

Share This Page