I am facing issue with proper date retrieval using date formatter.I have saved the date in the following format:
[dateFormat setDateFormat:@”YYYY-MM-dd HH:mm:ss”];
Now I need to display the saved date in view controller.For the convenience of readability and simplicity,I have retrieved the saved date from database and changed to a short format say January 17,February 18 etc..Here is the implementation code for that:
remind.Date = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 3)];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:remind.Date];
[dateFormat setDateFormat:@"MMMM dd"];
NSString *dateVal = [dateFormat stringFromDate:date];
remind.Date = dateVal;
Now the date value will be visible as shown in the following snap shot:

All these dates are saved in to an array called grpArray
Now I want to retrieve the complete date of formatted date,when I select the row and navigate to controller where that date is saved,for that in did select row at index path I implemented the following code:
-(void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ReminderClass *rem = [self.grpArray objectAtIndex:indexPath.section];
// Instantiate your detail/editor view controller,
// and pass in the ReminderClass object to be edited.
ERAddReminderViewController *rdvc = [[[ERAddReminderViewController alloc]initWithReminder:rem]autorelease];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:@"MMMM dd"];
[dateFormat setLenient:YES];
NSDate *date = [dateFormat dateFromString:rem.Date];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateVal = [dateFormat stringFromDate:date];
rem.Date = dateVal;
[self.navigationController pushViewController:rdvc animated:YES];
rdvc.navigationItem.rightBarButtonItem.title = @"Edit";
}
Now according to the logic I should get the complete date i.e. format specified.Its working fine but got a problem with correct year and time that was originally saved.Please see the following snap shot for clarification:

Now the date as we can see is 1970-01-17 00:00:00
But the actual date which I saved was 2012-01-17 19:40:40
What went wrong with the implementation
Can any one please guide me right
Thanks all in advance 🙂
What I see is you’re trying to convert a date
MMMM ddto ayyyy-MM-dd HH:mm:ss.Which ofcourse isn’t possible since the date in the first format doesn’t know the year and time.
That’s why it’s showing 1970 00:00:00 , the 01-17 are correct (ofcourse)
You always have to ‘store’ the original date
yyyy-MM-dd HH:mm:ssand work with that instead of the formatted one.