adrian
adrian

Reputation: 4594

NSDate returns null

I tried this:

NSDateFormatter *df= [[[NSDateFormatter alloc] init] autorelease];

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

NSDate *dt2 = [df dateFromString:@"2011-11-23 09:44:12"];

But when I do:

NSLog(@"dt2 is %@", dt2);

it returns null.Any idea why?

Upvotes: 0

Views: 316

Answers (2)

Parth Bhatt
Parth Bhatt

Reputation: 19469

Try this code:

NSDateFormatter *df= [[[NSDateFormatter alloc] init] autorelease];

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

NSDate *dt2 = [df dateFromString:@"2011-11-23 09:44:12"];

NSLog(@"dt2 is %@", dt2);

Here in HH:mm:ss, you had used MM for minutes instead of mm and SS for seconds in place of ss. That was the mistake you committed

Hope this helps you.

Upvotes: 5

Dennis Bliefernicht
Dennis Bliefernicht

Reputation: 5157

The basic reference for this is http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns which concludes that Parth's answer should be the right one.

Upvotes: 0

Related Questions