edhnb
edhnb

Reputation: 2202

NSDateFormatter dateFromString coming back null

I am trying to get a string date into a date object so I can do some comparisons. My code is as follows:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy/MM/dd_hh:mm:ss"];
NSDate *date = [formatter dateFromString:@"2012/02/06_20:35:59"]; 
[formatter release];

NSLog(@"date = %@", date);

My NSLog is showing that *date = null;

Any help is appreciated. Thanks.

Upvotes: 2

Views: 1738

Answers (2)

Dani Pralea
Dani Pralea

Reputation: 4561

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formatter dateFromString:@"2012-02-06 20:35:59"]; 
    [formatter release];

    NSLog(@"date = %@", date);


d Day of the month as digits; no leading zero for single-digit days.
dd Day of the month as digits; leading zero for single-digit days.
ddd Day of the week as a three-letter abbreviation.
dddd Day of the week as its full name.
m Month as digits; no leading zero for single-digit months.
mm Month as digits; leading zero for single-digit months.
mmm Month as a three-letter abbreviation.
mmmm Month as its full name.
yy Year as last two digits; leading zero for years less than 10.
yyyy Year represented by four digits.
h Hours; no leading zero for single-digit hours (12-hour clock).
hh Hours; leading zero for single-digit hours (12-hour clock).
H Hours; no leading zero for single-digit hours (24-hour clock).
HH Hours; leading zero for single-digit hours (24-hour clock).
M Minutes; no leading zero for single-digit minutes.
Uppercase M unlike CF timeFormat‘s m to avoid conflict with months.
MM Minutes; leading zero for single-digit minutes.
Uppercase MM unlike CF timeFormat‘s mm to avoid conflict with months.
s Seconds; no leading zero for single-digit seconds.
ss Seconds; leading zero for single-digit seconds.

Upvotes: 3

mattjgalloway
mattjgalloway

Reputation: 34912

Does't your format need to be yyyy/MM/dd_HH:mm:ss?

hh is 0-12 where as HH is 0-23.

For reference, here is the doc where I got that nugget of information from - http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

Upvotes: 6

Related Questions