How to get only the date value from a Windows Forms DateTimePicker control?
Asked Answered
E

10

83

I'm building an application with C# code.
How do I get only the date value from a DateTimePicker control?

Edema answered 16/7, 2009 at 14:53 Comment(0)
D
143

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");
Dygal answered 16/7, 2009 at 14:58 Comment(1)
So I've used yyyy-mm-dd instead of yyyy-MM-dd and was struggling why I get a random month every time I was trying to get a date :DIncondensable
F
52
DateTime dt = this.dateTimePicker1.Value.Date;
Fargone answered 16/7, 2009 at 14:58 Comment(1)
yes, but it picks 12:00 AM instead of the current time, and thus does the job.Idolist
A
9

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...

Apollyon answered 24/11, 2012 at 2:7 Comment(0)
L
5
string shortDate = dateTimePicker1.Value.ToShortDateString();
Lazarolazaruk answered 16/7, 2009 at 14:59 Comment(0)
M
4

Following that this question has been already given a good answer, in WinForms we can also set a Custom Format to the DateTimePicker Format 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

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

NOTE: If you are planning to insert Date only data to a date column type in SQL, see this documentation 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

Be aware. Cheers.

Milore answered 20/1, 2014 at 8:9 Comment(2)
Remember, custom format is case sensitive. It should be yyyy/dd/MM, Not yyyy/dd/mm.Backplate
i am getting Object reference not set to an instance of an object.'Garlic
P
1

You mean how to get date without the time component? Use DateTimePicker.Value.Date But you need to format the output to your needs.

Parapodium answered 16/7, 2009 at 15:0 Comment(0)
B
0

@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());
 } 
Basile answered 16/7, 2009 at 15:4 Comment(0)
V
0

Try this

var store = dtpDateTimePicker.Value.Date;

store can be anything entity object etc.

Variorum answered 17/3, 2015 at 7:49 Comment(0)
P
0

Easy, like it

string fecha = dtFecha.Value.ToString("yyyy/MM/dd");
Plagiarize answered 31/3, 2022 at 22:11 Comment(0)
L
-1
Datum = DateTime.Parse(DateTimePicker1.Value.ToString("dd/MM/yyyy"))
Louralourdes answered 10/4, 2015 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.