How to get only the date value from a Windows Forms DateTimePicker control?
C#WinformsDateDatetimepickerC# Problem Overview
I'm building an application with C# code.
How do I get only the date value from a DateTimePicker
control?
C# Solutions
Solution 1 - C#
I'm assuming you mean a datetime picker in a winforms application.
in your code, you can do the following:
string theDate = dateTimePicker1.Value.ToShortDateString();
or, if you'd like to specify the format of the date:
string theDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
Solution 2 - C#
DateTime dt = this.dateTimePicker1.Value.Date;
Solution 3 - C#
I had this issue when inserting date data into a database, you can simply use the struct members separately: In my case it's useful since the sql sentence needs to have the right values and you just need to add the slash or dash to complete the format, no conversions needed.
DateTimePicker dtp = new DateTimePicker();
String sql = "insert into table values(" + dtp.Value.Date.Year + "/" +
dtp.Value.Date.Month + "/" + dtp.Value.Date.Day + ");";
That way you get just the date members without time...
Solution 4 - C#
string shortDate = dateTimePicker1.Value.ToShortDateString();
Solution 5 - C#
Following that this question has been already given a good answer, in WinForms we can also set a [Custom Format][1] to the DateTimePicker [Format][2] property as Vivek said, this allow us to display the date/time in the specified format string within the DateTimePicker, then, it will be simple just as we do to get text from a TextBox.
// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "yyyy/MM/dd";
![dateTimePicker1][3]
We are now able to get Date only easily by getting the Text from the DateTimePicker:
MessageBox.Show("Selected Date: " + dateTimePicker1.Text, "DateTimePicker", MessageBoxButtons.OK, MessageBoxIcon.Information);
![enter image description here][4]
NOTE: If you are planning to insert Date only data to a date column type in SQL, see this [documentation][5] related to the supported String Literal Formats for date. You can not insert a date in the format string ydm because is not supported:
dateTimePicker1.CustomFormat = "yyyy/dd/MM";
var qr = "INSERT INTO tbl VALUES (@dtp)";
using (var insertCommand = new SqlCommand..
{
try
{
insertCommand.Parameters.AddWithValue("@dtp", dateTimePicker1.Text);
con.Open();
insertCommand.ExecuteScalar();
}
catch (Exception ex)
{
MessageBox.Show("Exception message: " + ex.Message, "DateTimePicker", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
the above code ends in the following Exception: ![enter image description here][6]
Be aware. Cheers. [1]: http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.customformat%28v=vs.110%29.aspx [2]: http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.format%28v=vs.110%29.aspx [3]: http://i.stack.imgur.com/CGo6D.png [4]: http://i.stack.imgur.com/pUEWZ.png [5]: http://msdn.microsoft.com/en-us/library/bb630352%28v=sql.110%29.aspx [6]: http://i.stack.imgur.com/89g00.png
Solution 6 - C#
You mean how to get date without the time component? Use DateTimePicker.Value.Date But you need to format the output to your needs.
Solution 7 - C#
@Shoban It looks like the question is tagged c# so here is the appropriate snipped http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.value.aspx
public MyClass()
{
// Create a new DateTimePicker
DateTimePicker dateTimePicker1 = new DateTimePicker();
Controls.Add(dateTimePicker1);
MessageBox.Show(dateTimePicker1.Value.ToString());
dateTimePicker1.Value = DateTime.Now.AddDays(1);
MessageBox.Show(dateTimePicker1.Value.ToString());
}
Solution 8 - C#
Try this
var store = dtpDateTimePicker.Value.Date;
store can be anything entity object etc.
Solution 9 - C#
Easy, like it
string fecha = dtFecha.Value.ToString("yyyy/MM/dd");
Solution 10 - C#
Datum = DateTime.Parse(DateTimePicker1.Value.ToString("dd/MM/yyyy"))