Mr Bonjour Mr Bonjour - 6 months ago 49
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.


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