Nikolay Ivanov Kalev
Nikolay Ivanov Kalev

Reputation: 11

subtract with dateTime

I am trying to subtract two dates from eachother in c#. I have left dateTimePicker and right dateTimePicker and textBox for the result. Can you show me the correct code?

private void dateTimePicker4_ValueChanged(object sender, EventArgs e)
        {
            int leftDateTime;
            int rightDateTime;
            dateTimePicker3.Value.Subtract - dateTimePicker4.Value.Subtract = textBox3;
        }

Upvotes: 0

Views: 60

Answers (1)

Tophandour
Tophandour

Reputation: 297

In C#, you can use the DateTime.Subtract(DateTime) method to subtract one date from another. See this page for documentation on this method: https://msdn.microsoft.com/en-us/library/8ysw4sby(v=vs.110).aspx

In your example, you can subtract the second date from the first date and place the results in the TextBox like this:

  textBox3.Text = dateTimePicker3.Value.Subtract(dateTimePicker4.Value).ToString();

Also, note that you have to put the item receiving the value on the left of the = and the value that you're giving it goes on the right.

Upvotes: 1

Related Questions