用法一:使用DateTimePicker控件显示时间
1.控件
DateTimePicker控件、textBox控件
2.代码
private void Form1_Load(object sender, EventArgs e)
{
//time时间
datTime.Format = DateTimePickerFormat.Time;//Format属性设置为Time
datTime.ShowUpDown = true;//ShowUpDown属性设为true
txtTime.Text = datTime.Text;
}
private void datTime_ValueChanged(object sender, EventArgs e)
{
txtTime.Text = datTime.Text;
}
3.注意:Format属性用于获取或设置控件中显示的日期和时间格式
其四个枚举值:
- Custom:DateTimePicker控件以自定义格式显示日期/时间值。
- Long:以用户操作系统设置的长日期格式显示日期/时间值
- Short:以用户操作系统设置的短日期/时间格式显示日期/时间值
- Time:以用户操作系统设置的时间格式显示日期/时间值
用法二:使用DateTimePicker控件以自定义格式显示日期
1.控件
DateTimePicker控件、textBox控件
2.代码