Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter dateFromString returning nil

I have the following code:

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    /*2010-11-02 20:31:39*/
[df setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSDate* date = [df dateFromString:@"2010-11-02 20:31:39"];

date is nil.

Any idea why?

like image 205
amitabh Avatar asked Nov 06 '10 23:11

amitabh


1 Answers

The hh symbol is used for hours between 1 and 12. Use HH for hours between 1 and 24. Also, YYYY is used for the week-numbering year. You probably want yyyy instead.

[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

Check out Unicode Technical Standard #35 for details.

like image 172
James Huddleston Avatar answered Oct 02 '22 14:10

James Huddleston