ArisRS
ArisRS

Reputation: 1394

UIDatePicker change locale

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?

Upvotes: 4

Views: 8937

Answers (3)

Leon
Leon

Reputation: 3726

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.

Upvotes: 0

Ahmed Abdallah
Ahmed Abdallah

Reputation: 2355

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

enter image description here

Upvotes: 1

Jeepston
Jeepston

Reputation: 1359

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

Upvotes: 0

Related Questions