Question

I'm sorry to make my first question here a bit of a simple one -- I've spent a day reading the NSLocale and NSCalendar class descriptions but I couldn't see if this was possible.

I have a UIDatePicker in the UIDatePickerModeDateAndTime mode. It is currently displaying date and time according to the user's locale, which is the default behavior.

I would like to be able to offer the option to show the UIDatePicker in either 12-hour or 24-hour time format. Detecting which time format the user is currently using isn't a problem, but I'm not clear on how to change just the time format of UIDatePicker without entirely throwing out the user's locale settings (since the picker also displays the localized days of the week and months). UIDatePicker supports setting its locale and setting its calendar.

So, question one is whether this is something I should be trying to do via NSLocale or NSCalendar, and question two is if anyone can recommend a way to isolate the time format without throwing out the rest of the user's locale settings.

Thanks for any suggestions.

Was it helpful?

Solution 2

OK, after reading the last two years' worth of others asking the same question here, it seems that there is no way to do this since the UIDatePicker uses the user's country setting instead of the user's locale setting, and that can't be overridden programmatically. I filed a bug.

OTHER TIPS

This is not the answer you are looking for, but in Cocoa you could create an NSDateFormatter and attach it to an NSDatePicker (which is an NSControl) using setFormatter. Unfortunately the equivalent iPhone class (UIControl) does not support this yet. I raised a bug with Apple about it and this is a known issue, although they wouldn't tell me if/when they plan to fix/enhance it.

I have solved this problem by using the user input of AM/PM by initializing another variable as a string of either AM or PM based upon the 12 hour format. The new hour in 24 hour format is put into NSCalendar. To wit:

var hra = 1//ENTER INITIAL HOUR (HH)

var dna = "PM"//ENTER "AM" OR "PM"

if hra <= 12 && dna == "PM"{

     hra = hra + 12
}

The variable hra now takes the value of the 24 hour format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top