How to get only date value from a Windows Forms DateTimePicker control?

I am building an application with C # code.
How to get only date value from DateTimePicker control?

+51
date c # winforms datetimepicker
Jul 16 '09 at 14:53
source share
9 answers

I assume you mean the datetime collector in the winforms application.

in your code, you can do the following:

 string theDate = dateTimePicker1.Value.ToShortDateString(); 

or, if you want to specify a date format:

 string theDate = dateTimePicker1.Value.ToString("yyyy-MM-dd"); 
+94
Jul 16 '09 at 14:58
source share
 DateTime dt = this.dateTimePicker1.Value.Date; 
+37
Jul 16 '09 at 14:58
source share

I had this problem when pasting date data into the database, you can just use the struct elements separately: In my case, this is useful since the sql clause must have the correct values, and you just need to add a slash or dash to complete the format, without conversions needed.

 DateTimePicker dtp = new DateTimePicker(); String sql = "insert into table values(" + dtp.Value.Date.Year + "/" + dtp.Value.Date.Month + "/" + dtp.Value.Date.Day + ");"; 

This way you only get date members without time ...

+8
Nov 24 :
source share
 string shortDate = dateTimePicker1.Value.ToShortDateString(); 
+3
Jul 16 '09 at 14:59
source share

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

+1
Jul 16 '09 at 15:00
source share

@Shoban It looks like the question is tagged with C #, so here is a suitable cut off 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()); } 
0
Jul 16 '09 at 15:04
source share

After this question has already received a good answer, in WinForms we can also set the Custom format to DateTimePicker Format , as Vivec said, this allows us to display the date / time in the specified format string in DateTimePicker, then it will be just as simple 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

Now we can easily get the date by getting the text from DateTimePicker:

 MessageBox.Show("Selected Date: " + dateTimePicker1.Text, "DateTimePicker", MessageBoxButtons.OK, MessageBoxIcon.Information); 

enter image description here

NOTE. If you plan to embed data only by date in the date column type in SQL, see this documentation for the supported string literary formats for the date . You cannot insert a date in a ydm format string because it 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 with the following Exception: enter image description here

Know. Greetings.

0
Jan 20 '14 at 8:09
source share

try it

 var store = dtpDateTimePicker.Value.Date; 

can be any object of an object, etc.

-one
Mar 17 '15 at 7:49
source share
 Datum = DateTime.Parse(DateTimePicker1.Value.ToString("dd/MM/yyyy")) 
-one
Apr 10 '15 at 16:04
source share



All Articles