I'm working on an history application so I need to cope with date before and after JC.
I'm trying to parse a string with the form "01/01/-200" but it returns a null date while it's working with "01/01/200".
Here is my code :
NSDateFormatter* dateFormatter = [[[NSDateFormatter allo开发者_如何学Cc]init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/y"]; // @TODO Get negative date
[dateFormatter setLenient:NO];
NSDate* date = [dateFormatter dateFromString:dateString];
return date;
I also try using with the form "01/01/200 BC" setDateFormat:@"dd/MM/y G"
but I can't make it work neither.
As mvds suggests in his answer, I tried the format "01/01/200 BC" on the simulator, and it's working... the problem only occurs on my iPad (version 3.2.1)
Do you have an idea how to do this properly ?
I just tried this:
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/y G"];
NSDate *date = [dateFormatter dateFromString:@"01/01/200 BC"];
NSLog(@"refdate %@",[dateFormatter stringFromDate:date]);
date = [date addTimeInterval:24*3600*365*2];
NSLog(@"2 years later %@",[dateFormatter stringFromDate:date]);
which outputs:
refdate 01/01/200 BC
2 years later 01/01/198 BC
This is on 3.2, iPad simulator, so not the most recent SDK, but iPad nonetheless. Do you get different results, running this?
I finally find the trick. The problem is that my iPad is in French so the Era has a different format :
- BC is "av. J.-C."
- AD is "ap. J.-C."
So I just had to change my XML file to get the correct format when parsing.
In order to display my date in the AD-BC format, I just convert it afterward :
+ (NSString*) convertIntoBCADString:(NSString*) originalString
{
NSString* newString = [originalString stringByReplacingOccurrencesOfString:@"av. J.-C." withString:@"BC"];
return [newString stringByReplacingOccurrencesOfString:@"ap. J.-C." withString:@"AD"];
}
精彩评论