Question

I need to cut off the time from the datepicker data. when i try to set the date as a label, the time is also appending to it. is there any way to cut off the time part from the picker data string. please help. Thanks in advance.

Was it helpful?

Solution 2

I got it working.

-(IBAction) timerButtonPressed:(id)sender
{
NSDate *newDate = [datePicker date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"HH:mm"];
dateField.text = [format stringFromDate:newDate];
}

Thanks for your information.

OTHER TIPS

You might want to try something like this:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

textLabel.text = [dateFormatter stringFromDate:picker.date];

NSDateFormatter allows you to format any date according to the current language. See the documentation for detail on which styles are available. To disable time use setTimeSytle with NSDateForammterNoStyle (see example).

How are you formatting the the datepicker data? And are you using this constant UIDatePickerModeDate? There are a lot of ways to format the data. Show me what you are doing currently so I can tell you how to trim the time off.

I recommend you take a look at the NSDateFormatter object. I've used this several times to format the results of my date picker.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top