4

I am trying to change locale for UIDatePicker like this:

  picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

It is working, but after setting locale the picker does not meet the system settings of time format. I mean 24hr/(AM/PM). What I am doing wrong?

ArisRS
  • 1,362
  • 2
  • 19
  • 41

3 Answers3

1

In xCode:- in local section you can set it;

enter image description here

Ahmed Abdallah
  • 2,338
  • 1
  • 19
  • 30
0

You are not doing anything wrong. Date/time format is dictated by locale. And en_US uses 12hr AM/PM format: http://demo.icu-project.org/icu-bin/locexp?_=en_US

Jeepston
  • 1,361
  • 7
  • 7
  • 1
    Thanks, But What I should do to support both time formats? The reason Why I changed time format was that picker shows my local language instead of currently setup english language. – ArisRS Apr 23 '13 at 13:44
  • Unfortunately, you cannot fine-tune date format of date picker. You can try setting several locales and calendars and choose what best fits your requirements. – Jeepston Apr 23 '13 at 13:49
0

The marked answer is not quite correct if you want to support the time format desired by the user.

If you set the picker locale to the default (that of the device) the picker will use the 12/24 hr format of that particular locale. In the US this would be 12hr and in the UK for example this would be 24hr.

However, if the user changes their device manually to 12/24 your picker will not use this style.

If your picker is only using time and not date+time you can resolve this using a couple of approaches:

  1. Detect if the device is 12/24 manually and then select locale en_US for 12hr and en_GB for 24hr.
  2. Don't set the locale, only set the timeStyle to NSDateFormatterShortStyle, this will automatically choose the time format selected from the device settings.

If you're using the date+time then you need to set a locale to that of the device to get the date in the right language and format but this will then ignore the time format set on the device.

Leon
  • 3,614
  • 1
  • 33
  • 46
  • Can you please explain in more detail? What do you mean with set the time format to `HH:mm`? Where should I set that? `UIDatePicker` has no `timeFormat`. – pre Aug 11 '17 at 13:56