A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-datetimepicker-class/ below:

C# - DateTimePicker Class - GeeksforGeeks

C# - DateTimePicker Class

Last Updated : 12 Jul, 2025

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.

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.

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.

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

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

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:

Constructor

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

Fields Fields Description DefaultMonthBackColor This field specifies the default month background colour of the DateTimePicker control. This field is read-only. DefaultTitleBackColor This field specifies the default title back colour of the DateTimePicker control. This field is read-only. DefaultTitleForeColor This field specifies the default title foreground colour of the DateTimePicker control. This field is read-only. DefaultTrailingForeColor This field specifies the default trailing foreground colour of the DateTimePicker control. This field is read-only. MaxDateTime This field specifies the maximum date value of the DateTimePicker control. This field is read-only. MinDateTime This field gets the minimum date value of the DateTimePicker control. Properties Properties Description AutoSize This property is used to get or set a value that indicates whether the control resizes based on its contents. AutoSizeMode This property indicates the automatic sizing behaviour of the control. BackColor This property is used to get or set the background colour for the control. BorderStyle This property indicates the border style for the control. CalendarFont This property is used to get or set the font style applied to the calendar. CalendarForeColor This property is used to get or set the foreground colour of the calendar. CalendarMonthBackground This property is used to get or set the background colour of the calendar month. CalendarTitleBackColor This property is used to get or set the background colour of the calendar title. CalendarTitleForeColor This property is used to get or set the foreground colour of the calendar title. CalendarTrailingForeColor This property is used to get or set the foreground colour of the calendar trailing dates. Font This property is used to get or set the font of the text displayed by the control. ForeColor This property is used to get or set the foreground colour of the control. Format This property is used to get or set the format of the date and time displayed in the control. Height This property is used to get or set the height of the control. Location This 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. MaxDate This property is used to get or set the maximum date and time that can be selected in the control. MaximumDateTime This property is used to get the maximum date value allowed for the DateTimePicker control. MinDate This property is used to get or set the minimum date and time that can be selected in the control. MinimumDateTime This property is used to set the minimum date value allowed for the DateTimePicker control. Name This property is used to get or set the name of the control. ShowUpDown This 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. ShowCheckBox This property is used to get or set a value indicating whether a check box is displayed to the left of the selected date. Size This property is used to get or set the height and width of the control. Visible This property is used to get or set a value indicating whether the control and all its child controls are displayed. Value This property is used to get or set the date/time value assigned to the control. Width This 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.

Handling Common Issues and Errors

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



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4