强制培养 24 小时和 12 小时时间格式

Forcing in culture 24h and 12h time format

如题。我试过这个:

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;

我想更改工具包时间选择器上的时间格式

我假设你有 System.Windows.Forms.DateTimePicker.

如果是这样,您无需更改当前线程的区域性 - 您可以只使用 CustomFormat 属性,例如

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

注:

我已将 ShowUpDown 设置为 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.