How to: Display Time with the DateTimePicker Control

How to: Display Time with the DateTimePicker Control


If you want your application to enable users to select a date and time, and to display that date and time in the specified format, use the DateTimePicker control. The following procedure shows how to use the DateTimePicker control to display the time.

To display the time with the DateTimePicker control

  1. Set the Format property to Time 

    timePicker.Format = DateTimePickerFormat.Time
  2. Set the ShowUpDown property for the DateTimePicker to true.

    timePicker.ShowUpDown = True

The following code sample shows how to create a DateTimePicker that enables users to choose a time only.

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Public Sub New()

    End Sub
    Private timePicker As DateTimePicker

    Private Sub InitializeTimePicker()
        timePicker = New DateTimePicker()
        timePicker.Format = DateTimePickerFormat.Time
        timePicker.ShowUpDown = True
        timePicker.Location = New Point(10, 10)
        timePicker.Width = 100

    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New Form1())

    End Sub
End Class

Compiling the Code

This example requires:

  • References to the System, System.Data, System.Drawing and System.Windows.Forms assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

© 2015 Microsoft