VictorT
VictorT

Reputation: 783

NSDate and NSDateFormatter magic

I faced with strange problem while converting NSString value of date to NSDate instance. It looks like Great Harry Potter visited my code.

I receive date string from server as 24-th date format: "2011-11-15 11:44". My iPhone has 12-th date format in settings. When I try to convert that string to NSDate I can see that date value is equal to NIL. If I switch settings to 24-th date format it works fine. This is my code:

NSString * sDate = @"2011-11-15 11:44";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm"];
[df setTimeZone:[NSTimeZone localTimeZone]];
NSDate * pDate = [df dateFromString:sDate];

As result, pDate is NIL. But, if I run this code on iPod Touch I can see wrong date value in 12-th format instead NIL. I can't understand a problem. Could you help me please?

Thanks.

Upvotes: 0

Views: 275

Answers (1)

Daniel Albert
Daniel Albert

Reputation: 757

I checked Apple's documentation and you have to set the locale to en_US_POSIX. You can see more info at this link:

http://developer.apple.com/library/ios/#qa/qa1480/_index.html

Upvotes: 2

Related Questions