In this article I will explain with an example, how to get only Time from DateTimePicker (Calendar) in Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
The following form consists of a DateTimePicker and a Button control.
The DateTimePicker has been assigned with the following properties.
Format – Determines whether dates and times are displayed using standard or custom formatting. In this case it is set to Time.
ShowUpDown – Indicates whether a spin box rather than a drop-down calendar is displayed for modifying the control value. In this case it is set to True.
Getting only Time from DateTimePicker in C# and VB.Net
The following event handler is executed, when Submit button is clicked.
Inside this event handler, the selected Time is displayed in Message Box.
C#
private void OnSubmit(object sender, EventArgs e)
{
DateTime dt = Convert.ToDateTime(dateTimePicker1.Value);
MessageBox.Show("Selected Time: " + dt.ToShortTimeString());
}
VB.Net
Private Sub OnSubmit(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim dt As DateTime = Convert.ToDateTime(dateTimePicker1.Value)
MessageBox.Show("Selected Time: " & dt.ToShortTimeString())
End Sub
Screenshot
Demo
Downloads