Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 406
srinivas Avatar asked Jul 16 '09 14:07

srinivas


People also ask

What does DateTimePicker return?

It returns a DateTime value DateTime date1 = datetimepicker1.Value; Follow this answer to receive notifications.

How do I use DateTimePicker in Windows form?

We can create a DateTimePicker control using the Forms designer at design-time or using the DateTimePicker class in code at run-time (also known as dynamically). To create a DateTimePicker control at design-time, you simply drag and drop a DateTimePicker control from Toolbox to a Form in Visual Studio.

How do I convert DatePicker to time?

Solution 3. string str = string. Format("dd-MM-yyyy"); string ab = date. ToString(str);

Which control is used as a DatePicker?

The DatePicker control allows the user to select a date by either typing it into a text field or by using a drop-down Calendar control. Many of a DatePicker control's properties are for managing its built-in Calendar, and function identically to the equivalent property in Calendar. In particular, the DatePicker.


6 Answers

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");
like image 80
tardomatic Avatar answered Oct 05 '22 10:10

tardomatic


DateTime dt = this.dateTimePicker1.Value.Date;
like image 43
Keith Avatar answered Oct 05 '22 12:10

Keith


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

like image 34
Gustave Santiago Avatar answered Oct 05 '22 11:10

Gustave Santiago


string shortDate = dateTimePicker1.Value.ToShortDateString();
like image 23
waqasahmed Avatar answered Oct 05 '22 11:10

waqasahmed


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.

like image 43
WhySoSerious Avatar answered Oct 05 '22 12:10

WhySoSerious


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

like image 38
Vivek Avatar answered Oct 05 '22 10:10

Vivek