UPDATE: As pointed out by @rmaddy, there was a misunderstanding about what visit.olcreatedat
was.
This first answer is based on the assumption that visit.olcreatedat
is an
NSDate. For an answer that assumes that visit.olcreatedat
is an NSString containing a formatted date, read the second part of this answer.
visit.olcreatedat is an NSDate
- (void)viewDidLoad {
[super viewDidLoad];
// Alloc the date formatter in a method that gets run only when the controller appears, and not during scroll operations or UI updates.
self.outputDateFormatter = [[NSDateFormatter alloc] init];
[self.outputDateFormatter setDateFormat:@"EEEE, dd MMM yyyy HH:mm:ss z"];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath: {
// dequeue or create the cell...
// ...
// ...
OLVisit *visit = [self.visits objectAtIndex:indexPath.row];
NSString * strVisitDate = [self.outputDateFormatter stringFromDate:visit.olcreatedat];
// Here you can assign the string to the right UILabel
cell.textLabel.text = strVisitDate;
return cell;
}
visit.olcreatedat is an NSString
Do you want to convert the visit.olcreatedat
to the format EEEE, dd MMM yyyy HH:mm:ss z? If so, let's go through each step.
First, you need to convert the visit.olcreatedat
string (stored in strVisitDate
) to an actual NSDate object, visitDate
. So to make this step work, you need an NSDateFormatter that accepts the visit.olcreatedat
format: yyyy-MM-dd HH:mm:ss z.
Then, you want to convert the obtained visitDate
NSDate to the format you like, so you need another NSDateFormatter, with the format EEEE, dd MMM yyyy HH:mm:ss z.
- (void)viewDidLoad {
[super viewDidLoad];
// Alloc the date formatter in a method that gets run only when the controller appears, and not during scroll operations or UI updates.
self.inputDateFormatter = [[NSDateFormatter alloc] init];
[self.inputDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
self.outputDateFormatter = [[NSDateFormatter alloc] init];
[self.outputDateFormatter setDateFormat:@"EEEE, dd MMM yyyy HH:mm:ss z"];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath: {
// dequeue or create the cell...
// ...
// ...
OLVisit *visit = [self.visits objectAtIndex:indexPath.row];
NSString *strVisitDate = visit.olcreatedat;
NSDate *visitDate = [self.inputDateFormatter dateFromString:strVisitDate];
strVisitDate = [self.outputDateFormatter stringFromDate:visitDate];
// Here you can assign the string to the right UILabel
cell.textLabel.text = strVisitDate;
return cell;
}