0

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;
        }
Tophandour
  • 297
  • 5
  • 15
  • Here you go: [Beginners book for .NET and C#?](http://stackoverflow.com/questions/2994959/beginners-book-for-net-and-c) – Marshal Nov 20 '15 at 15:36

1 Answers1

1

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.

Tophandour
  • 297
  • 5
  • 15
  • You might consider pointing out that his x-y=z statement does not work by itself and should be z=x-y (as you did it in your answer) – xXliolauXx Nov 20 '15 at 15:20