dsyden
dsyden

Reputation: 3

Issues with String to NSDate

My dateFromString is not working and i'm not sure why

NSString *purchase = @"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];

date is 'invalid CFStringRef'

See anything i might be overlooking?

Upvotes: 0

Views: 262

Answers (4)

zaph
zaph

Reputation: 112857

in the format
"hh" means Hour [1-12].
"HH" means Hour [0-23].

See UTS Date Field Symbol Table for the date format specifiers.

Upvotes: 1

chown
chown

Reputation: 52738

Try this:

NSString *purchase = @"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];

Date Formatters Guide - HH is used for 24 hour times, hh for 12 hour.

Upvotes: 1

Circumflex
Circumflex

Reputation: 710

Try changing the hours in the formatter to capitals, i.e. yyyy-MM-dd HH:mm:ss

I'm not sure if that will solve your error, but that is the correct way to parse a 24 hour time

Upvotes: 2

Hot Licks
Hot Licks

Reputation: 47729

If you mean that "date" is not accepted as an NSString by another function, that's because it's not a string, it's an NSDate.

(Where, precisely, are you getting the error message, and what is the full text?)

Upvotes: 0

Related Questions