Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
DateTime.SpecifyKind() Method in C#
Next article icon

C# | DateTimePicker Class

Last Updated : 05 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

In Windows Forms, the DateTimePicker control is used to select and display the date/time with a specific format in your form. The FlowLayoutPanel class is used to represent windows DateTimePicker control and also provide different types of properties, methods, and events. It is defined under System.Windows.Forms namespace. You can create two different types of DateTimePicker, as a drop-down list with a date represented in the text, or as a calendar which appears when you click on the down-arrow next to the given list. In C#, you can create a DateTimePicker in the windows form by using two different ways:

1. Design-Time: It is the easiest way to create a DateTimePicker control as shown in the following steps:

  • Step 1: Create a windows form as shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp

  • Step 2: Next, drag and drop the DateTimePicker control from the toolbox to the form as shown in the below image:

  • Step 3: After drag and drop you will go to the properties of the DateTimePicker to modify DateTimePicker according to your requirement.

    Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a DateTimePicker programmatically with the help of syntax provided by the DateTimePicker class. The following steps show how to set the create DateTimePicker dynamically:

  • Step 1: Create a DateTimePicker using the DateTimePicker() constructor is provided by 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: And last add this DateTimePicker control to the form and also add other controls on the DateTimePicker using the following statements:
      // Adding this control   // to the form   this.Controls.Add(d);   

    Example:




    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp48 {
      
    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

Constructor Description
DateTimePicker() This constructor is used to initializes a new instance of the DateTimePicker class.

Fields

Fields Description
DefaultMonthBackColor This is field specifies the default month background color of the DateTimePicker control. This field is read-only.
DefaultTitleBackColor This is field specifies the default title back color of the DateTimePicker control. This field is read-only.
DefaultTitleForeColor This is field specifies the default title foreground color of the DateTimePicker control. This field is read-only.
DefaultTrailingForeColor This is field specifies the default trailing foreground color of the DateTimePicker control. This field is read-only.
MaxDateTime This is field specifies the maximum date value of the DateTimePicker control. This field is read-only.
MinDateTime This is field get the minimum date value of the DateTimePicker control.

Properties

Property 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 behavior of the control.
BackColor This property is used to get or set the background color 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 color of the calendar.
CalendarMonthBackground This property is used to get or set the background color of the calendar month.
CalendarTitleBackColor This property is used to get or set the background color of the calendar title.
CalendarTitleForeColor This property is used to get or set the foreground color of the calendar title.
CalendarTrailingForeColor This property is used to get or set the foreground color 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 color 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.


Next Article
DateTime.SpecifyKind() Method in C#
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-Windows-Forms-Namespace

Similar Reads

  • How to set a Check Box in the DateTimePicker in C#?
    In Windows Form, the DateTimePicker control is used to select and display date/time with a specific format in your form. In DateTimePicker control, you are allowed to set a checkbox in the DateTimePicker using the ShowCheckBox Property. If the value of this property is set to true, then a checkbox d
    3 min read
  • How to set Maximum Date in the DateTimePicker in C#?
    In Windows Forms, the DateTimePicker control is used to select and display date/time with a specific format in your form. In DateTimePicker control, you can set the maximum date and time that can be selected in the DateTimePicker using the MaxDate Property. The default value of this property is Dece
    3 min read
  • How to set Minimum Date in the DateTimePicker in C#?
    In Windows Forms, the DateTimePicker control is used to select and display date/time with a specific format in your form. In DateTimePicker control, you can set the minimum date and time that can be selected in the DateTimePicker using the MinDate Property. The default value of this property is 1/1/
    3 min read
  • DateTime.ToString() Method in C# | Set – 1
    This method is used to Converts the value of the current DateTime object to its equivalent string representation. There are total 4 methods in the overload list of this method: ToString(String, IFormatProvider)ToString(String)ToString(IFormatProvider)ToString() Here, we will discuss only first two m
    8 min read
  • DateTime.SpecifyKind() Method in C#
    This method is used to create a new DateTime object which has the same number of ticks as the specified DateTime but is designated as either local time, Coordinated Universal Time (UTC), or neither, as indicated by the specified DateTimeKind value. Syntax: public static DateTime SpecifyKind (DateTim
    2 min read
  • How to display Current Date/Time in the DateTimePicker in C#?
    In Windows Forms, the DateTimePicker control is used to select and display date/time with a specific format in your form. In DateTimePicker control, you can set the current Date/Time using the Value Property. or in other words, we can say that Value property is used to assign a Date/Time value to th
    3 min read
  • How to set the Name of the DateTimePicker in C#?
    In Windows Forms, the DateTimePicker control is used to select and display date/time with a specific format in your form. In DateTimePicker control, you can set the name of the DateTimePicker on the form using the Name Property. You can set this property in two different ways: 1. Design-Time: It is
    3 min read
  • How to set the Size of the DateTimePicker in C#?
    In Windows Forms, the DateTimePicker control is used to select and display date/time with a specific format in your form. In DateTimePicker control, you can set the size of the DateTimePicker using the Size Property. This property represents both height and width in pixels. You can set this property
    3 min read
  • How to set the Font of the DateTimePicker in C#?
    In Windows Forms, the DateTimePicker control is used to select and display date/time with a specific format in your form. In DateTimePicker control, you can set the font of the DateTimePicker on the form using the Font Property. This property is an ambient property. You can set this property in two
    3 min read
  • How to set the Format of the DateTimePicker in C#?
    In Windows Forms, the DateTimePicker control is used to select and display date/time with a specific format in your form. In DateTimePicker control, you can set the format of the date and time that displayed in the DateTimePicker the Format Property. The values of this property are defined under Dat
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences