Export (0) Print
Expand All
2 out of 3 rated this helpful - Rate this topic

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.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TimePickerApplication
{
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeTimePicker();
        }
        private DateTimePicker timePicker;

        private void InitializeTimePicker()
        {
            timePicker = new DateTimePicker();
            timePicker.Format = DateTimePickerFormat.Time;
            timePicker.ShowUpDown = true;
            timePicker.Location = new Point(10, 10);
            timePicker.Width = 100;
            Controls.Add(timePicker);
        }
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

    }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.