问题
EDIT: For future reference: found this for NSDateFormatter. This documentation shows the different code to display the proper date and time format you are looking for. Cheers.
http://www.alexcurylo.com/blog/2009/01/29/nsdateformatter-formatting/
I am creating an app that uses a UIDatePicker to select a date AND time for an event (there is an option for this in the storyboard).
I want to know how I can display both the date AND the time in a label after selection/save. Right now, this is what I have for portions of my code throughout my app. My question is: do I change the formatter code to allow the UIDatePicker to display the date AND time in my app? Is there a way to do this? Thanks in advance!
MasterViewController
-(void)configureCell:(MasterEventCell *)cell atIndexPath:(NSIndexPath *)indexPath{
static NSDateFormatter *formatter = nil;
if (formatter == nil) {
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
}
Event *eventAtIndex = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.eventDetailsLabel.text = eventAtIndex.detail;
cell.eventLocationLabel.text = eventAtIndex.location;
cell.eventDateTimeLabel.text = [formatter stringFromDate:(NSDate *)eventAtIndex.date];
}
DetailViewController
- (void)configureView
{
Event *theEvent = self.event;
static NSDateFormatter *formatter = nil;
if (formatter == nil) {
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
}
if (theEvent) {
self.detailLabel.text = theEvent.detail;
self.locationLabel.text = theEvent.location;
self.dateTimeLabel.text = [formatter stringFromDate:(NSDate *)theEvent.date];
}
}
回答1:
Call setTimeStyle:
too on the date formatter.
来源:https://stackoverflow.com/questions/20459746/uidatepicker-has-a-date-and-time-option-but-how-to-display-both-in-an-app