Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter dateFromString

The string which comes from my data source is formatted like this:

2011-04-11 23:12:05

// dateString comes from my data source, and look like I've said like this.
dateString = @"2011-04-11 23:12:05";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];

NSDate *date = [formatter dateFromString:dateString];

When I've searched internet after threads like these the answer has always been that the dateString had wrong format. But my dateString is one of the correct ones, right?

Why does date returns nil?

like image 434
Fernando Redondo Avatar asked Feb 04 '11 07:02

Fernando Redondo


1 Answers

A date format string of @"MMMM" means that the string you're trying to parse is the full name of a month ("January", "October", etc).

You want something more like:

yyyy-MM-dd HH:mm:ss

Check out the date formatting patterns for more info.

like image 133
Dave DeLong Avatar answered Oct 18 '22 13:10

Dave DeLong