Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i put just the date from a dateTimePicker into a variable?

I’m using C# and WinForms to try and put just the date from a dateTimePicker into a variable. But I keep getting the date and the time. In the example below textBox1 shows what I’m looking for. But I can’t figure out how to do the same for textBox2. Where it gives the date and time. This isn’t the exact code I’m using in my app, but a simplified example of it for use as an example. How can I modify it to do what I need? Thanks.

    private void dateTimePicker1_CloseUp(object sender, EventArgs e)
    {
        DateTime varDate;

        textBox1.Text = dateTimePicker1.Value.ToShortDateString();

        varDate = dateTimePicker1.Value;
        textBox2.Text = Convert.ToString(varDate);
    }
like image 696
JimDel Avatar asked Dec 03 '22 15:12

JimDel


2 Answers

varDate = dateTimePicker1.Value.Date;

This will return the DateTimePicker's value with the time set to 00:00:00

like image 92
Thomas Levesque Avatar answered May 01 '23 01:05

Thomas Levesque


Try this

textBox2.Text = varDate.ToShortDateString();

or

varDate = DateTimePicker1.Value.Date;
textBox2.Text = varDate.ToShortDateString() // varDate.ToString("MM/dd/yy") this would also work
like image 32
Cody C Avatar answered May 01 '23 01:05

Cody C