Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rajneesh.deora

macrumors newbie
Original poster
Aug 19, 2008
21
0
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,
 
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,


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
 
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,

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];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.