8

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?

Breeze
  • 2,010
  • 2
  • 32
  • 43
amitabh
  • 81
  • 1
  • 3

1 Answers1

21

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.

James Huddleston
  • 8,410
  • 5
  • 34
  • 39
  • [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSDate *date = [formatter dateFromString:dateTxt]; not working where dateTxt = 2013-04-03 15:39:54 – Nik Apr 11 '13 at 09:58
  • yes James is Correct, "yyyy" is used for representing years in a date and "YYYY" is used for getting the week number for the particular data –  Jun 01 '16 at 07:05