Reputation: 637
I am new to C#. I am using windows forms.
I want to set my dateTimePicker to 14:30:00
time when button is clicked.
I already changed the dateTimePicker1 property so it only shows time:
dateTimePicker1.Formate = Time
I know how to set it to the current time by using: "dateTimePicker1.Value = DateTime.Now"
but how do I set the time to 14:30:00
when button is clicked?
Any idea? thank you
Upvotes: 0
Views: 4311
Reputation: 1006
You can simply create a new DateTime:
DateTime time = new DateTime();
And then add the appropriate time to it.
time.AddHours(14);
time.AddMinutes(30);
Or if you want less code.
time.AddMinutes((14*60)+30);
Upvotes: 1
Reputation: 189
You can do this with
dateTimePicker1.Formate = new DateTime(2015, 12, 11, 14, 30, 0)
DateTime(year, month, day, hour, min, sec, milli sec)
And if you allways want to get the current day you can do this with that:
DateTime s = DateTime.Now;
TimeSpan ts = new TimeSpan(14, 30, 0);
s = s.Date + ts;
Console.WriteLine(s);
The output is 11.12.2015 14:30
Upvotes: 4
Reputation: 939
Do you mean like this?
private void button1_Click(object sender, EventArgs e)
{
dateTimePicker1.Value = new DateTime(2000, 1, 1, 14, 30, 0);
}
Upvotes: 2