Table of contents
1.
Introduction
2.
DateTimePicker Class
2.1.
Design-Time
2.2.
Run-Time
2.2.1.
Step-1
2.2.2.
Step 2
2.2.3.
Step 3
3.
Properties of DateTimePicker control
3.1.
Name
3.2.
Location
3.3.
Size
3.4.
Height and Width
3.5.
Font
3.6.
Text and Value
3.7.
MinDate and MaxDate
3.8.
Calendar Foreground and Background
3.9.
ShowCheckBox and ShowUpDown
3.10.
Custom DateTimePicker
3.11.
DateTimePicker ValueChanged Event Handler
4.
Frequently Asked Questions
4.1.
What is DateTimePicker class?
4.2.
How can we change the properties of DateTimePicker control?
4.3.
How to display the current date and time in the windows form?
4.4.
How to display time with the DateTimePicker control?
4.5.
How to display a date in a custom format with the Windows forms DateTimePicker control?
5.
Conclusion
Last Updated: Mar 27, 2024

C# DateTimePicker Class

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

You might know about windows form. Windows Form is a UI framework for building Windows desktop applications. It is one of the most productive ways to create a desktop app. In this article, we will learn about the DateTimePicker Class provided in Windows Form.

Recommended Topic, Palindrome in C#, singleton design pattern in c# and Ienumerable vs Iqueryable

DateTimePicker Class

This class is used to select, display, and collect the date and time in different formats in our windows forms. It is defined under the System.Windows.Forms namespace. The FlowLayoutPanel class provides various properties, methods, and events for the DateTimePicker control. We can create two different types of DateTimePicker. The first one is a drop-down menu with the date and time written in text format. And the second one is a calendar format. They appear when clicked on the down arrow beside the given list.

Source

In C#, we can create a DateTimePicker in two different ways given below.

Design-Time

It is easy to create a DateTimePicker control in design time. As shown below, we can drag and drop the DateTimePicker control from the toolbox.

This will get us a DateTimeControl in our form like this:

Source

We can modify the DateTimePicker according to our needs by changing its properties.

Source

Run-Time

Using this method, we can create a DateTimePicker programmatically. However, this method is a bit trickier than the previous one. We can follow the below-given steps to do so.

Step-1

Create a DateTimePicker using the DateTimePicker() constructor provided by the DateTimePicker class.

DateTimePicker ninja = new DateTimePicker();

Step 2

After creating the DateTimePicker, we can its properties provided by the DateTimePicker class. We have given the code below to customize some of the properties. The working of these properties is obvious from their names but don't worry if you cannot understand their work as we have discussed each one of them further in this article.

ninja.Location = new Point(360, 162); 
ninja.Size = new Size(292, 26); 
ninja.MaxDate = new DateTime(2500, 12, 20); 
ninja.MinDate = new DateTime(1753, 1, 1); 
ninja.Format = DateTimePickerFormat.Long;
ninja.Name = "MyPicker";
ninja.Font = new Font("Comic Sans MS", 12);
ninja.Visible = true;
ninja.Value = DateTime.Today;

Step 3

We have created and modified our DateTimePicker according to our needs. Now, the most crucial step. Add it to your form! Let us see how we can do that.

this.Controls.Add(ninja);

This code snippet adds our control to the form.

Properties of DateTimePicker control

We have discussed above that we can set the properties of the DateTimePicker according to our needs. The easiest way is to use the Properties window, which can be accessed by right-clicking on the control and selecting properties from it. This will display a property window like the given below.

Source

Each of these properties can be set dynamically during run-time using a code snippet. We have discussed the code snippets for various properties, and their functions are given below.

Name

This property refers to a unique name for the DateTimePicker. It is used to access the control in the code. It can be set using the below-given code snippet.

DateTimePicker dynamicDTP = new DateTimePicker();  
dynamicDTP.Name = "DyanmicDateTimePicker";

Location

Location property specifies the starting position of the DateTimePicker control on a form. It can be customized using the below-given code.

DateTimePicker dynamicDTP = new DateTimePicker(); 
dynamicDTP.Location = new System.Drawing.Point(12, 12);

Size

It specifies the size of the DateTimePicker control. The code for customizing it on run-time is given below. In this code, we have set the control's height as 25 and the control's width as 200.

DateTimePicker dynamicDTP = new DateTimePicker();  
dynamicDTP.Size = new System.Drawing.Size(200, 25);

Height and Width

The height and width property can be used in place of the size property to alter the control size. The code for the same is given below.

DateTimePicker dynamicDTP = new DateTimePicker();
dynamicDTP.Width = 200;  
dynamicDTP.Height = 25; 

Font

It represents the font of the text to be displayed in the DateTimePicker control. The below-given code changes the font property at run-time. In this code, the font family is set to ''Georgia,'' and the font size is set to ''12''.

DateTimePicker dynamicDTP = new DateTimePicker();
dynamicDTP.Font = newFont("Georgia", 12);

Text and Value

Text property refers to the control text, and the Value property refers to the currently set date and time value in the DateTimePicker control. Code for using these properties is given below.

DateTimePicker dynamicDTP = new DateTimePicker();
MessageBox.Show(dynamicDTP.Text);  
MessageBox.Show(dynamicDTP.Value.ToString());

MinDate and MaxDate

MinDate and MaxDate properties are used to set the minimum and maximum value of the date and time selected in control. The code to change these properties at run-time is given below. In this code, we set the minimum date 01-01-2000 and the maximum date today.

DateTimePicker dynamicDTP = new DateTimePicker();
dynamicDTP.MinDate = newDateTime(2000, 1, 1);  
dynamicDTP.MaxDate = DateTime.Today;

Calendar Foreground and Background

We have an option to change the properties of the calendar part of the DateTimePicker control. We can change properties like CalendarFont, CalendarForeColor, CalendarMonthBackground, CalendarTitleBackColor, and CalendarTitleBackColor. All these properties are self-explanatory, i.e., it is obvious from their name what they do. We can change these calendar properties using the below-given code snippet.

DateTimePicker dynamicDTP = new DateTimePicker();
dynamicDTP.CalendarFont = newFont("Tahoma", 10F, FontStyle.Italic,  
GraphicsUnit.Point, ((Byte)(0)));  
dynamicDTP.CalendarForeColor = Color.Black;  
dynamicDTP.CalendarMonthBackground = Color.Orange;  
dynamicDTP.CalendarTitleBackColor = Color.Yellow;  
dynamicDTP.CalendarTitleForeColor = Color.Black;

ShowCheckBox and ShowUpDown

The value of the DateTimePicker control can be changed without using the calendar. This can be done by setting the ShowCheckBox property to true. It makes the date part of the control editable. ShowUpDown property enables us to change the date and time using a spin button control. We can use these two properties using the below-given code.

DateTimePicker dynamicDTP = new DateTimePicker();
dynamicDTP.ShowCheckBox = true;  
dynamicDTP.ShowUpDown = true; 

Custom DateTimePicker

Suppose the date is June 01, 2001, and we want to display the date as "June 01, 2001 - Friday". For this, we can use the CustomFormat property. If we're going to show the date and time as "June 15 at 12:00 PM," then to display the date and time separators or format the strings, we must use escape characters in the substring. To show "June 15 at 12:00 PM," we have to set the CustomFormat property to "MMMM dd ''at'' t: mm tt." We have given a code snippet below to make you understand its implementation.

dynamicDTP.Format = DateTimePickerFormat.Custom;
dynamicDTP.CustomFormat = "MMMM dd, yyyy - dddd";

For more information on custom DateTimePicker, you can refer to the documentation by Microsoft.

DateTimePicker ValueChanged Event Handler

This event handler is fired when the value of the control is changed. This is important in cases where we need to change some values based on the control value. To add this property, we can go to events windows and double click on the ValueChanged event as given below.

Source

Another way to implement this event is using this code snippet.

DateTimePicker dynamicDTP = new DateTimePicker();
dynamicDTP.ValueChanged += new System.EventHandler(dateTimePicker1_ValueChanged);  
privatevoid dateTimePicker1_ValueChanged(object sender, EventArgs e)  
{  
    MessageBox.Show("New Datetime :" + dynamicDTP.Value.ToString());
// code of task you want to perform on change of control value
}

Frequently Asked Questions

What is DateTimePicker class?

DateTimePicker class is used to select, display, and collect the date and time in different formats in our windows forms.

How can we change the properties of DateTimePicker control?

Its properties can be changed in two ways. We can change it during the design time from the properties window or change it during run-time programmatically.

How to display the current date and time in the windows form?

We can use the below-given code snippet to get the current date and time.

DateTime now = DateTime.Now;

Here ''now'' stores the current date and time.

How to display time with the DateTimePicker control?

To display time with the DateTimePicker control, set the ''format'' property to ''Time'' and set the ''ShowUpDown'' property for the DateTimePicker to ''true''. You can refer to the below-given code snippet to do so.

timePicker.Format = DateTimePickerFormat.Time;
timePicker.ShowUpDown = true;

How to display a date in a custom format with the Windows forms DateTimePicker control?

To display a custom format, set the Format property to DateTimePickerFormat.Custom and set the CustomFormat property to a format string like "ddd dd MMM yyyy."

Conclusion

In this article, we have extensively discussed C# DateTimePicker Class. We hope that this blog has helped you enhance your knowledge regarding C# DateTimePicker Class and if you would like to learn more, check out our articles on  

Refer to our carefully curated articles and videos and code studio library if you want to learn more. Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!!!

Live masterclass