Last Updated: December 26, 2018
·
5.748K
· _max_k

MySQL 'timestamp' string to NSDate

Quick and easy tutorial to show you how you can convert the MySQL 'timestamp' data type as a string to an NSDate variable.

NSString *dateAsString = [self.userInfo objectForKey:@"last_updated"];

/* @dateAsString is the MySQL timestamp as a string */

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

/* the date format is the same as that used by MySQL timestamps */

NSDate *date = [formatter dateFromString:dateAsString]; 

/* as if by magic, the date is returned into the 'date' variable */

[formatter release];