Open In App

C# - DateTimePicker Class

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Windows Forms, the DateTimePicker control is used to select and display the date or time in a specific format. Defined in System.Windows.Forms namespace. The DateTimePicker class in C# is used to represent the Windows DateTimePicker control and also provides different types of properties, methods, and events.

Why Use DateTimePicker?

When we type data manually, there are chances of errors, but when we use DateTimePicker, it makes sure that the users select valid dates in a proper and correct format. This will reduce the errors and also improve the user experience.

Ways to Create a DateTimePicker In Windows Forms

There are primarily two ways to create a DateTimePicker in Windows Forms, which are mentioned below.

  • Drag and drop (Design-Time)
  • Custom DateTimePicker (Run-Time)

1. Drag and drop (Design-Time)

This is the easiest way to create a DateTimePicker in Windows Forms using Visual Studio. We just have to open the toolbox and drag and drop the DateTimePicker on the form in the designer, and further, we can change the appearance of the DateTimePicker using the properties. Follow these steps to create a DateTimePicker.

Step 1: Locate your Windows Forms project (e.g., Form1). We are using the default name Form1 in this example and it will open a form in the editor that we can further modify.

Empth-forms

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form1.cs file for the custom logic.


Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

ToolBox


Step 3. Now open the common controls and drag and drop the DateTimePicker on the form where we want it to be placed.

DateTimePickerImage


Step 4. Now open the properties right-click on the DateTimePicker and it will open the Properties window where you can configure its settings.

Properties


Output:

Output


2. Custom DateTimePicker (Run Time)

In this method, we are going to modify the Form1.cs file and add custom code modification in C# with the help of the DateTimePicker class. The following steps show how to create a DateTimePicker dynamically:

Step 1: Create a DateTimePicker using the DateTimePicker() constructor of the DateTimePicker class.

// Creating a DateTimePicker

DateTimePicker d = new DateTimePicker();


Step 2: After creating a DateTimePicker, set the properties of the DateTimePicker provided by the DateTimePicker class.

// Setting the location of the DateTimePicker

d.Location = new Point(360, 162);


// Setting the size of the DateTimePicker

d.Size = new Size(292, 26);


// Setting the maximum date of the DateTimePicker

d.MaxDate = new DateTime(2500, 12, 20);


// Setting the minimum date of the DateTimePicker

d.MinDate = new DateTime(1753, 1, 1);


// Setting the format of the DateTimePicker

d.Format = DateTimePickerFormat.Long;


// Setting the name of the DateTimePicker

d.Name = "MyPicker";


// Setting the font of the DateTimePicker

d.Font = new Font("Comic Sans MS", 12);


// Setting the visibility of the DateTimePicker

d.Visible = true;


// Setting the value of the DateTimePicker

d.Value = DateTime.Today;


Step 3: Add the DateTimePicker and other controls on the DateTimePicker using the following statements:

// Adding this control

// to the form

this.Controls.Add(d);


Step 4: Now double-click on the form in Design and it will open the Form1.cs file where code is written in C#. Here, the program file is Form 1.cs Now write this code in Form1.cs file

Form1.cs file:

C#
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the 
            // properties of the Label 
            Label l = new Label();
            l.Location = new Point(183, 162);
            l.Size = new Size(172, 20);
            l.Text = "Select Date and Time";
            l.Font = new Font("Comic Sans MS", 12);

            // Adding this control 
            // to the form 
            this.Controls.Add(l);

            // Creating and setting the 
            // properties of the DateTimePicker 
            DateTimePicker d = new DateTimePicker();
            d.Location = new Point(360, 162);
            d.Size = new Size(292, 26);
            d.MaxDate = new DateTime(2500, 12, 20);
            d.MinDate = new DateTime(1753, 1, 1);
            d.Format = DateTimePickerFormat.Long;
            d.Name = "MyPicker";
            d.Font = new Font("Comic Sans MS", 12);
            d.Visible = true;
            d.Value = DateTime.Today;

            // Adding this control 
            // to the form 
            this.Controls.Add(d);


        }

    }
}


Output:

Output


Constructor

DateTimePicker(): This constructor is used to initialise a new instance of the DateTimePicker class.

Fields

FieldsDescription
DefaultMonthBackColorThis field specifies the default month background colour of the DateTimePicker control. This field is read-only.
DefaultTitleBackColorThis field specifies the default title back colour of the DateTimePicker control. This field is read-only.
DefaultTitleForeColorThis field specifies the default title foreground colour of the DateTimePicker control. This field is read-only.
DefaultTrailingForeColorThis field specifies the default trailing foreground colour of the DateTimePicker control. This field is read-only.
MaxDateTimeThis field specifies the maximum date value of the DateTimePicker control. This field is read-only.
MinDateTimeThis field gets the minimum date value of the DateTimePicker control.


Properties

PropertiesDescription
AutoSizeThis property is used to get or set a value that indicates whether the control resizes based on its contents.
AutoSizeModeThis property indicates the automatic sizing behaviour of the control.
BackColorThis property is used to get or set the background colour for the control.
BorderStyleThis property indicates the border style for the control.
CalendarFontThis property is used to get or set the font style applied to the calendar.
CalendarForeColorThis property is used to get or set the foreground colour of the calendar.
CalendarMonthBackgroundThis property is used to get or set the background colour of the calendar month.
CalendarTitleBackColorThis property is used to get or set the background colour of the calendar title.
CalendarTitleForeColorThis property is used to get or set the foreground colour of the calendar title.
CalendarTrailingForeColorThis property is used to get or set the foreground colour of the calendar trailing dates.
FontThis property is used to get or set the font of the text displayed by the control.
ForeColorThis property is used to get or set the foreground colour of the control.
FormatThis property is used to get or set the format of the date and time displayed in the control.
HeightThis property is used to get or set the height of the control.
LocationThis property is used to get or set the coordinates of the upper-left corner of the DateTimePicker control relative to the upper-left corner of its form.
MaxDateThis property is used to get or set the maximum date and time that can be selected in the control.
MaximumDateTimeThis property is used to get the maximum date value allowed for the DateTimePicker control.
MinDateThis property is used to get or set the minimum date and time that can be selected in the control.
MinimumDateTimeThis property is used to set the minimum date value allowed for the DateTimePicker control.
NameThis property is used to get or set the name of the control.
ShowUpDownThis property is used to get or set a value indicating whether a spin button control (also known as an up-down control) is used to adjust the date/time value.
ShowCheckBoxThis property is used to get or set a value indicating whether a check box is displayed to the left of the selected date.
SizeThis property is used to get or set the height and width of the control.
VisibleThis property is used to get or set a value indicating whether the control and all its child controls are displayed.
ValueThis property is used to get or set the date/time value assigned to the control.
WidthThis property is used to get or set the width of the control.

Real-World Use Cases of DateTimePicker

Now, let's discuss some real world use cases where we can use DataTimePicker.

  • Users can easily pick dates and times for meetings without to worry about how to type them.
  • It also makes sure that the users enter correct dates like birthdays, joining dates, or deadlines etc.
  • It also lets user pick start and end dates to narrow down the data they want to see in reports.


Handling Common Issues and Errors

With the help of DataTimePicker we can reduce mistakes, but it's still good to check:

  • The date users pick to make sure it’s not in the past or outside allowed limits.
  • Make sure there are no empty or missing dates when saving to the database. As we know DateTimePicker does not support empty dates by default.
  • Watch out for problems when saving or loading dates in different formats.

Similar Reads