What am I doing wrong?
I am trying to getNSDateFormatter to translate custom patterns for dates using the current locale.
Example:
dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [NSLocale currentLocale];
[dateFormat setDateFormat:#"MMM"];
output = [self.dateFormat stringFromDate:dateObject];
No matter what I change my current locale settings to, I always see the English month abbreviations.
Thanks for any help you can provide.
It looks like it was working all of the time. I had been leaving my location set to United States and changing only the language. To get it to work correctly you have to change the location as well as the language.
Related
Here is how I am localising days:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *dayFormat = [NSDateFormatter dateFormatFromTemplate:#"EEEE" options:0 locale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:dayFormat];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *day = [dateFormatter stringFromDate:date];
And yet it seems to be returning English days of the week (Monday, Tuesday etc) rather than the device language (which has been set to German in the simulator).
Any idea where I'm going wrong?
Update after doing some research on device I've realised that its actually the region setting, not the language setting which changes the date language. Odd, but I guess its done for a reason.
Thanks
The language of the date is set by the region not the language. This has to be a bug. If I'm in Germany, but an English speaker I don't want to have my dates in German, surely?
Anyway, this is why. You have to change language and region.
I've had the same issue. It wouldn't work on the simulator, but it would on a device. Can you try it ? I did not however solve it, I did not even look more into it as it was working perfectly on the device, which is truly the main target of your app.
Edit:
This comes from Apple's doc:
currentLocale
Returns the logical locale for the current user.
+ (id)currentLocale
Return Value
The logical locale for the current user. The locale is formed from the settings for the current user’s chosen system locale overlaid with any custom settings the user has specified in System Preferences.
Discussion
Settings you get from this locale do not change as a user’s
Preferences are changed so that your operations are consistent.
Typically you perform some operations on the returned object and then
allow it to be disposed of. Moreover, since the returned object may be
cached, you do not need to hold on to it indefinitely. Contrast with
autoupdatingCurrentLocale.
Maybe you can try using:
preferredLanguages
Returns the user's language preference order as an array of strings.
+ (NSArray *)preferredLanguages
Return Value
The user's language preference order as an array of NSString objects, each of which is a canonicalized IETF BCP 47 language identifier.
How do I keep the date format fix getting from UIDatePickerView, It should not affected from country(Region Format). Presently In my application if I set the region format as "China" it is displaying UIDatePickerView in local china format. I want to as it is but I want to access the date from this in my fixed standard format. It should not be affect by country whether it is Australia or China or UK. Please Suggest with example.
I do not want to change the date format in Picker View just want to change date format access from it. Here is the code that I am using:
NSDateFormatter *datef = [[NSDateFormatter alloc]init];
NSCalendar *usersCalendar =
[[NSLocale currentLocale] objectForKey:NSLocaleCalendar];
[datef setLocale:[NSLocale currentLocale]];
[datef setTimeZone:[NSTimeZone localTimeZone]];
[datef setCalendar:usersCalendar];
tempFDate = [datef dateFromString:[NSString stringWithFormat:#"%# %#",txtDate.text,txtTime.text]];
NSLog(#"%#",tempFDate);
I am continuously getting null in console. I need such a date format that can work for all the region formats. Please Suggest its urgent for me.
NSDateformatter is the best option.
See, the timezone if not specified it will use the timezone of which device is set and display the content accordingly
there are options to get the device setted timezone also to work with it
+ localTimeZone
+ defaultTimeZone
+ setDefaultTimeZone:
+ resetSystemTimeZone
+ systemTimeZone
check this class
Here you need the reponse to a certain timezone for eg GMT 0000
then
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
will do the job for you
using NSDateFormatter.
you can use NSDateFormatter to convert any date format like dd/MM/yyyy etc.
My App is translated into several languages, but for now I want to force the language to Dutch. I did force the language to Dutch by setting the AppleLanguages key in the standardUserDefaults to Dutch. This is working for localized strings and xib files.
But the NSDateFormatter seems to ignore this value completely. It's just using the iPhones en_US locale to format the date, even if I set the locale myself on the NSDateFormatter object:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *dutch = [[NSLocale alloc] initWithLocaleIdentifier:#"nl_NL"];
[dateFormatter setLocale:dutch];
[dutch release];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
dateLabel.text = [dateFormatter stringFromDate:[BabyInfo getDate]];
[dateFormatter release];
Does anyone have a clue how to solve this?
Update: So it seems the all the date stuff (like NSDateFormatter and NSDatePicker) are not using the language locale to determine what type of date to show, but are looking at the location locale. Still, I don't know how to tell the formatter and picker that I want them to show their dates in Dutch. So any help is still welcome, but at least I now understand what the problem is.
Maybe this answer comes too late but check this out:
http://www.alexcurylo.com/blog/2011/02/16/tip-nsdateformatter-localization/
This will force the Formatter/Picker to use the locale with the preferred language without having to change your iPhone/iPad settings.
To use the selected language for your date formatter use:
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
According to this site:
http://iosdevelopertips.com/cocoa/date-formatter-examples.html
there is a class that handles formatting, which takes in a set of constants/enums (e.g. NSDateFormatterShortStyle) to the "setDateStyle" property/method.
Somehow the NSDateFormatter knows to retrieve the proper locale-specific date format. What I want is to be able to retrieve the default formats based on the user's choice of region format. I have a feeling it is stored in NSLocale, but that does not seem to expose anything that will retrieve the format strings.
Is there a way to extract the formats? It has to be in memory somewhere; I'm hoping the retrieval mechanism is exposed somewhere.
I've looked in several places, but the only answers I get are a lesson on how to create an NSDate from a custom format.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle];
NSString *dateFormat = [df dateFormat];
NSLog(#"Date format: %#", dateFormat);
[df release];
Just tested on OS X but this should also work in iOS.
In particular I'm interested in shortStandaloneWeekdaySymbols. I'm using this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSArray *shortWeekdaySymbols = [dateFormatter shortStandaloneWeekdaySymbols];
But if iPhone region format is set to US but language to French/German/any other, NSDateFormatter returns English strings (mon tue ...). But I want to respect the language settings and get weekday names in the current language (standard Clock app does this for example). Is this possible?
The only way I can think of to do this would be to get the current user language (how?) and set locale on the date formatter to this language's region.
The only way you can think of, "get the current user language (how?) and set locale on the date formatter to this language's region" is correct. This is how.
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]] autorelease]];
Try to look here
Detecting current iPhone input language