I have an NSDate object from which I make two NSStrings: The date and the time. Currently I format the date as 20111031 and time as 23:15.
What I would like to do is to format it to the device (iPhone, iPad, iPod Touch) current region settings (not the language!). So for instance:
How can I do this?
The following should be enough, because an NSDateFormatter has the phone's default locale by default:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(@"%@",[dateFormatter stringFromDate:date]);
FYI here's what happens with US, Netherlands, and Sweden:
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
NSLog(@"%@",[dateFormatter stringFromDate:date]);
// displays 10/30/11 7:09 PM
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"nl_NL"]];
NSLog(@"%@",[dateFormatter stringFromDate:date]);
// displays 30-10-11 19:09
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"]];
NSLog(@"%@",[dateFormatter stringFromDate:date]);
// displays 2011-10-30 19:09