How can I set a DateTimePicker control to a specific date?

Viewed 182024

How can I set a DateTimePicker control to a specific date (yesterday's date) in C# .NET 2.0?

9 Answers

Just need to set the value property in a convenient place (such as InitializeComponent()):

    dateTimePicker1.Value = DateTime.Today.AddDays(-1);

You can set the "value" property

dateTimePicker1.Value = DateTime.Today;

This oughta do it.

DateTimePicker1.Value = DateTime.Now.AddDays(-1).Date;

Use the Value property.

MyDateTimePicker.Value = DateTime.Today.AddDays(-1);

DateTime.Today holds today's date, from which you can subtract 1 day (add -1 days) to become yesterday.

DateTime.Now, on the other hand, contains time information as well. DateTime.Now.AddDays(-1) will return this time one day ago.

dateTimePicker1.Value = DateTime.Today();

Also, we can assign the Value to the Control in Designer Class (i.e. FormName.Designer.cs).

DateTimePicker1.Value = DateTime.Now;

This way you always get Current Date...

FYI: If you are setting the value, and not seeing anything - you might check to see if you have a 'CustomFormat' set - I just hit this and it was set to ' ' for the 1/1/1900 value (our 'not set' value) and set to MM/dd/yyyy if not.

Related