Task: Show a UIDatePicker and grab the selected date, then display the selected date in a label (in the format of Day, Month, Year).
Current progress:
-(IBAction)pressButton:(id)sender
{
NSDate *selected = [datePicker date];
NSString *message = [[NSString alloc] initWithFormat:@"%@", selected];
date.text = message;
}
This will display the date in the format of YYYY-MM-DD 23:20:11 +0100. I do not want to display the time. I also want to display the date in a different format.
Is it possible to access the individual components of the date picker ie. datePicker.month
Any solutions or links is greatly appreciated.
What you want is the
descriptionWithCalendarFormat:timeZone:locale:method. See Apple’s description of the method.For instance, to display the date in just the YYYY-MM-DD format, you’d use:
I believe the format string here uses the same tokens as strptime from the C standard library, but there may be some minor discrepancies, knowing Apple. A description of that format is here.
You could also use the NSDateFormatter class’ stringFromDate: method. It uses a different format (the Unicode format). I believe it is the “preferred” way to format date strings in Objective C, but it’s probably a bit more complicated to use as well.
Finally, see this SO question for information on extracting individual NSDate components.
Hope that helps.