Mr Bonjour Mr Bonjour - 1 year ago 105
Objective-C Question

ios, why NSDateFormatter suck so bad?

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE"]; // or E, EE, EEE

NSLog(@"--> %@", [dateFormat @"Wed"]); // --> Null

[dateFormat setDateFormat:@"EEEE"];
NSLog(@"--> %@", [dateFormat @"Wednesday"]); // --> Null


** EDIT **
Before downvoting my question, here is my problem:

I took and exemple from stackoveflow:

NSString *dateStr = @"Tue, 25 May 2010 12:53:58 +0000"; // Convert string to date object

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
NSLog(@"--> %@", [dateFormat dateFromString:dateStr]);

And the same code at home, return me null.

If I transform to "25 2010 12:53:58" -> "d yyyy HH:mm:ss", I get a correct date object. My NSDateFormatter isn't able to transform EEE and LLL conversion.

Answer Source

You need to teel the dateformatter in which language the string will be:

NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = enLocale;
[dateFormat setDateFormat:@"EEE"]; // or E, EE, EEE

NSLog(@"--> %@", [dateFormat dateFromString:@"Wed"]); // --> Null
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download