views:

546

answers:

5

Hi, 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);
    }
+6  A: 
varDate = dateTimePicker1.Value.Date;

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

Thomas Levesque
This will still give him the time in textBox2 since he uses Convert.ToString().
Sani Huttunen
Thanks Thomas, I tried that but didn't realize that the time was reset to 0 as you mention. For my purposes this is fine, and will do what I'm looking for. Thanks.
JimDel
+4  A: 

textBox2.Text = varDate.ToShortDateString();

or

varDate = DateTimePicker1.Value.Date; textBox2.Text = varDate.ToShortDateString() // varDate.ToString("MM/dd/yy") would also work

Cody C
+1 There is no delineation of date and time in the DateTime type, and there needn't be. It's just a matter of outputting it the way you want/need.
SnOrfus
+1  A: 

Like this:

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

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

    varDate = dateTimePicker1.Value;
    textBox2.Text = varDate.ToShortDateString();
}
Sani Huttunen
A: 

textBox1.Text = dateTimePicker1.Value.ToString("yyyy-MM-dd");

Phill
+1  A: 

Thanks to Lazarus' comment above I think I understand your question now...

There is no way for a DateTime variable to only hold a Date value, the best you can do is set the time part to 00:00:00. Unfortunately this will not stop it being printed out when you call the default ToString() method. Therefore the method to solve your issue will entirely depend on what the actual problem you are trying to solve is:

If you are trying to display only the Date part in a TextBox then you can use the ToShortDateString() method, or any other date formatting string, but I guess you already know this, since you are using that method with TextBox1.

If you are trying to compare two datetimes and don't want the time part to be relevant you can use the Date property to ensure that the time part is set to midnight.

datetimeA.Date == dateTimeB.Date

Note that this doesn't avoid the comparison of the time part (the objects being compared are still Date*times*, but it does ensure that they are equal.

The only other reason that I can think you may wish to do this is because you feel that the object is too complicated and contains extraneous information. While I very much doubt this is the issue it may comfort you to know that the internals of a DateTime object are little more than a single long value, and you are not wasting space with the unused time portion.

Martin Harris
Hi Martin, I AM looking to compare dates as you've said. And making sure that i can essentially zero out both times will help me accomplish my goal. So thank you too for you answer!
JimDel