how we can get the time from UIDatePicker

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

  1. rajneesh.deora macrumors newbie

    Joined:
    Aug 19, 2008
    #1
    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
    [dateFormatter stringFromDate:timePicker.date]
    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...
    Raj,
     
  2. Fontano macrumors member

    Joined:
    Jun 27, 2008
    #2

    Code:
    [yourlable setText:[datePicker.date 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

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

    Code:
    
    -(void)createPicker
    {
    	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 stringFromDate:myPickerView.date]];
    	[df release];
    }
    
     

Share This Page