DateTimePicker and seconds

2019-03-04 02:14发布

问题:

I've searched SO, but could't get the answer. I use a DateTimePicker on a WinForms dialog and specify a custom format: dd.MM.yyyy HH:mm:ss. But DateTimePicker doesn't show the seconds, it always shows 00 for them. Even if I type e.g. "15" seconds, the value is correct but it's shown wrong.

What did I do wrong?

回答1:

I've found out the answer by myself. If I bind the data source to the Text property of DateTimePicker, the seconds will not be shown. I have to bind to the Value property instead.



回答2:

Did you set the Format property before specifying your custom format?

public void SetMyCustomFormat()
{
   // Set the Format type and the CustomFormat string.
   dateTimePicker1.Format = DateTimePickerFormat.Custom;
   dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
}

Example cadged from the MSDN page for DateTimePicker.CustomFormat Property