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

Viewed 366720

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

10 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");
DateTime dt = this.dateTimePicker1.Value.Date;
string shortDate = dateTimePicker1.Value.ToShortDateString();

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

@Shoban It looks like the question is tagged c# so here is the appropriate snipped 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());
 } 

Easy, like it

string fecha = dtFecha.Value.ToString("yyyy/MM/dd");
Related