I am trying to convert string into NSDate using following code.
NSString *old = [[NSUserDefaults standardUserDefaults] valueForKey:@"OLD_DATE"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy"];
NSDate *oldDate = [formatter dateFromString:old];
NSLog(@"Old Date : %@",oldDate);
I am setting OLD_DATE as following :
NSDate *oldDate = [NSDate date];
NSString *old = [[NSString stringWithFormat:@"%@",oldDate] retain];
[[NSUserDefaults standardUserDefaults] setObject:old forKey:@"OLD_DATE"];
But I am getting null in the NSLog(@"Old Date : %@",oldDate); 🙁
I found many similar links with the same code and works fine for them.
So what could be problem with my code ?
Don’t covert the date to string:
To read out the date just:
There really should not be any reason to concert the date to a string before saving to in the
NSUserDefaults.But if you want to concert the string back to a date you will need the correct format.
A NSDate descrption would be something like
2011-11-11 12:58:36 +0000