Brett
Brett

Reputation: 12007

iPhone SDK - NSDateFormatter - NSDate from string always returns nil

So this sounds like it should be so simple, yet for me, this code block returns a nil date:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *newStartDate = [df dateFromString:@"2012-05-11 20:45:00"];

Does anyone have any idea what could be causing newStartDate to be returned as nil? df is valid, the format string appears to be valid. I'm completely stuck here, so any advice would be great!

Thanks!

Upvotes: 0

Views: 226

Answers (2)

cnu
cnu

Reputation: 815

you should use a valid date

its better if you take something like

NSDate *d = [NSDate date];

or it should be 2012-05-11 20:45:00 +0000

your current static date is missing timezone

Upvotes: -2

giorashc
giorashc

Reputation: 13713

change hh to HH. hh is for 1-12 with AM/PM HH is for 0-23.

Upvotes: 5

Related Questions