How to set DateTimePicker at a specific time and save the customized date?

In C # form windows, I want to set DateTimePickerto a specific one timeand keep it tuned date(any one dateI choose from DateTimePicker) when the button is pressed.

eg:

Let's say that we have this date and time: 2015-08-15 08:30:00I want the button to dateremain the same and timechange to 11:30:00.

I mean, if I select any dateof DateTimePicker, and then press the button, it datewill remain the same and timechange to 11:30:00.

Please help me achieve this.

I may need to modify the following code to achieve what I want:

private void button1_Click(object sender, EventArgs e)
    {
        dateTimePicker1.Value = DateTime.Today.AddHours(11.5); // shows you  current date and 11:30:00 time


    }


    private void button1_Click(object sender, EventArgs e)
    {
        dateTimePicker1.Value = DateTime.Now; // shows you  current date and time

    }
+4
source share
2

, ,

private void button1_Click(object sender, EventArgs e)
{
    DateTimePicker1.CustomFormat = "MM/dd/yyyy hh:mm tt";

    DateTimePicker1 .Value = new DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
}

, , , custome formate.

    private void button2_Click(object sender, EventArgs e)
{
    DateTimePicker1.CustomFormat = "MM/dd/yyyy hh:mm tt";

    DateTimePicker1.Value = new DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day, 15, 23, 0);
}

3:23 .

,

DateTimePicker1.Value = DateTime.Now; .

DateTime.Now.Year

DateTime.Now.Month

DateTime.Now.Day

, datetime, .

+1

- . datetimepickers , . , , . , dtpDate dtpTime,

    private void dtpTime_ValueChanged(object sender, EventArgs 
    {
        dtpDate.Value = dtpDate.Value.Date + dtpTime.Value.TimeOfDay
    }
    private void dtpDate_ValueChanged(object sender, EventArgs 
    {
        dtpTime.Value = dtpDate.Value.Date + dtpTime.Value.TimeOfDay
    }
+2

Source: https://habr.com/ru/post/1619930/


All Articles