Search code examples
c#timewindows-phonetimepickerculture

Forcing in culture 24h and 12h time format


As in title. I tried this:

CultureInfo ti = CultureInfo.CurrentCulture;

if (SettingsApp[0].TimeFormat == 0)
{
    ti.DateTimeFormat.ShortTimePattern = "HH:mm";
}
else
{
    ti.DateTimeFormat.ShortTimePattern = "hh:mm tt";
}

Thread.CurrentThread.CurrentCulture = ti;

I want to change the time format on toolkit timepicker


Solution

  • I assume you have a System.Windows.Forms.DateTimePicker.

    If so, you need not change the culture on the current thread - you can just use the CustomFormat property, e.g.

    public void SetCustomFormat(DateTimePicker dateTimePicker, bool twentyFourHour)
    {
       dateTimePicker.ShowUpDown = true;
       dateTimePicker.Format = DateTimePickerFormat.Custom;
       dateTimePicker.CustomFormat = twentyFourHour ? "HH:mm" : "hh:mm tt";
    }
    

    Note:

    I've set ShowUpDown to true:

    When the ShowUpDown property is set to true, a spin button control (also known as an up-down control), rather than a drop-down calendar, is used to adjust time values.