Today my question is about date and string formats. My application downloads some strings representing dates from the Internet. The date format is always this: "2010-05-24 at 20:45" I need to convert this string to an NSDate object to do some date manipulation. I tried this code:
NSString * dateString = @"2010-05-24 at 20:45"
NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
NSDate * dateFromString = [myDateFormatter dateFromString:dateString];
It seems to work fine as long as the iPhone is set to 24 hours. While I switch to 12 hours in general settings, the NSDate object is not created (and the application happily crashes) I read others that have similar problems, but it seems I have not found a solution. Now the questions: For me, it does not make sense that NSDateFormatter is reasonable for user settings, as long as I indicate which format it should use to parse the string (which is not modified since it comes from the Internet), what use is the user settings check? More importantly, how can I accomplish my task (to get an NSDate object from a string, regardless of user preferences)?