Have the following piece of code in my app:
All the NSLog statements are added to try and work out the problem here. What this does is take my input date, which is of format '3/29 8:48pm', add the year to the string and then run it through the formatter to output it in the 'yyyy-MM-dd'T'HH:mm:SSSSSS' format. This all works perfectly in all cases except when the device's regional setting has been set to Japan. For some reason then the 'NSDate *datetime = [outputDateFormatter dateFromString:[NSString stringWithFormat
"%@-%@",year,tmpLastPostDate]];' just gives me a nil datetime.
Here's the output from the NSLog statement on the line below:
Again, this works perfectly for any other region I've tested other than some of the Asian ones.
So what is wrong here? Is this a bug in NSDateFormatter or am I doing something wrong? I am specifically telling it what format it should be expecting the date in, so why is this being affected by the region setting?
Code:
NSDate *today = [NSDate date];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:@"yyyy"];
NSString *year = [outputDateFormatter stringFromDate:today];
[outputDateFormatter setDateFormat:@"yyyy-MM/dd' 'hh:mmaa"];
NSDate *datetime = [outputDateFormatter dateFromString:[NSString stringWithFormat:@"%@-%@",year,tmpLastPostDate]];
NSLog(@"DateFormat: %@\nString: %@\nDate: %@",[outputDateFormatter dateFormat],[NSString stringWithFormat:@"%@-%@",year,tmpLastPostDate],datetime);
NSLog(@"Pre: %@",tmpLastPostDate);
[outputDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:SSSSSS"];
tmpLastPostDate = [outputDateFormatter stringFromDate:datetime];
NSLog(@"Post: %@",tmpLastPostDate);
[outputDateFormatter release];
All the NSLog statements are added to try and work out the problem here. What this does is take my input date, which is of format '3/29 8:48pm', add the year to the string and then run it through the formatter to output it in the 'yyyy-MM-dd'T'HH:mm:SSSSSS' format. This all works perfectly in all cases except when the device's regional setting has been set to Japan. For some reason then the 'NSDate *datetime = [outputDateFormatter dateFromString:[NSString stringWithFormat
Here's the output from the NSLog statement on the line below:
DateFormat: yyyy-MM/dd' 'hh:mmaa
String: 2010-3/30 11:36pm
Date: (null)
Again, this works perfectly for any other region I've tested other than some of the Asian ones.
So what is wrong here? Is this a bug in NSDateFormatter or am I doing something wrong? I am specifically telling it what format it should be expecting the date in, so why is this being affected by the region setting?