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);
}
varDate = dateTimePicker1.Value.Date;
This will return the DateTimePicker's value with the time set to 00:00:00
Try this
textBox2.Text = varDate.ToShortDateString();
or
varDate = DateTimePicker1.Value.Date;
textBox2.Text = varDate.ToShortDateString() // varDate.ToString("MM/dd/yy") this would also work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With