views:

159

answers:

1
//NSString *compileDate = [NSString stringWithFormat:@"%s", __DATE__];
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];

[df setDateFormat:@"MMM d yyyy"];   
//[df setDateFormat:@"MMM dd yyyy"];    

NSDate *aDate = [df dateFromString:compileDate];  

Ok, I give up. Why would aDate sometimes return as nil?

Should it matter if I use the commented-out lines... or their matching replacement lines?

+1  A: 

It can return nil if the phone's Region setting is not US (or equivalent).

Try setting the formatter's locale to en_US:

NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"MMM d yyyy"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[df setLocale:usLocale];
[usLocale release];
NSDate *aDate = [df dateFromString:compileDate];  
DyingCactus
Wow. I'll try that. I thought __DATE__ would always be determined during compile-time... and based on the machine I was compiling it on (English). So __DATE__ would *ALWAYS* be in "Feb 1 2010" format.(I'm not asking for the date to be determined during run-time... or based on the user's local setting.)
Susanna
The `__DATE__` macro is determined at compile time and replaced with a string literal in that format. But your conversion of that string into an NSDate happens at run-time which is affected by the current region.
DyingCactus