C#选择DateTimePicker时隐藏日期

时间:2018-12-02 18:41:44

标签: c# .net datetime

我正在尝试使用C#的“ DateTimePicker”创建月份选择器:

enter image description here

问题是,datetimepicker也显示日期。

有没有一种方法可以“隐藏”表格中的日期并仅显示月份和年份?

1 个答案:

答案 0 :(得分:1)

您可以在Format上设置CustomFormatDateTimePicker属性以自定义显示。要显示完整的月份名称,请使用MMMM,对于4个字符的年份,请使用yyyy

您可以了解有关自定义DateTime格式字符串here的更多信息。

DateTimePicker dateTimePicker1 = new DateTimePicker
{
    Format = DateTimePickerFormat.Custom,
    CustomFormat = "MMMM yyyy",
    ShowUpDown = true
};

Controls.Add(dateTimePicker1);

输出:

![![enter image description here