You are on page 1of 3

C# DateTimePicker Control

The DateTimePicker control allows you to display and collect date


and time from the user with a specified format.

The DateTimePicker control has two parts, a label that displays the
selected date and a popup calendar that allows users to select a
new date. The most important property of the DateTimePicker is the
Value property, which holds the selected date and time.
dateTimePicker1.Value = DateTime.Today;

The Value property contains the current date and time the control is
set to. You can use the Text property or the appropriate member of
Value to get the date and time value.
DateTime iDate;
iDate = dateTimePicker1.Value;

The control can display one of several styles, depending on its


property values. The values can be displayed in four formats, which
are set by the Format property: Long, Short, Time, or Custom.
dateTimePicker1.Format = DateTimePickerFormat.Short;

Convert String to DateTime


You can use the methods like Convert.ToDateTime(String),
DateTime.Parse() and DateTime.ParseExact() methods for
converting a string-based date to a System.DateTime object. More
about..... String to DateTime
How to find date difference ?
The DateTime.Substract method may be used in order to find the
date-time difference between two instances of the DateTime
method. More about..... Find date difference
How to to set datetime object to null ?
By default DateTime is not nullable because it is a Value Type, using
the nullable operator introduced in C# 2, you can achieve this. More
about..... Datetime object to null
The following C# program shows how to set and get the value of a
DateTimePicker1 control.
Next : C# Treeview Control
Download Source Code
Print Source Code
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
dateTimePicker1.Value = DateTime.Today;
}

private void button1_Click(object sender, EventArgs e)


{
DateTime iDate;
iDate = dateTimePicker1.Value;
MessageBox.Show("Selected date is " + iDate);
}
}

You might also like