-4

I have three text boxes where I can enter time like on this screenshot:

Form screenshot

Finally I want to get the difference of entry time (t1), exit time (t2) and break (t3): t1 - t2 - t3.

But I am not able to do that because of error message:

Operator '-' cannot be applied to operands of type 'System.TimeSpan' and 'System.DateTime'

My code is as follows:

DateTime t1, t2, t3,  reserve;
t1 = DateTime.Parse(inBox.Text); //from text box1 eg.11:30
t2 = DateTime.Parse(outBox.Text); //from text box2 eg.12:30
t3 = DateTime.Parse(breakBox.Text); //from text box3 eg.0:30
TimeSpan diffTime = t2 - t1 -t3;// there is a problem doing this!!!
answerLabel.Text = diffTime.ToString();
t3chb0t
  • 16,340
  • 13
  • 78
  • 118

2 Answers2

0

How about this?

DateTime t1, t2, t3,  reserve;

t1 = DateTime.Parse(inBox.Text); //from text box1 eg.11:30
t2 = DateTime.Parse(outBox.Text); //from text box2 eg.12:30
t3 = DateTime.Parse(breakBox.Text); //from text box3 eg.0:30

TimeSpan diffTime1 = t1 - t2;
TimeSpan diffTime2 = t2 - t3;

TimeSpan totalSpan = diffTime1 + diffTime2;

answerLabel.Text = diffTime2.ToString();
macrae2100
  • 62
  • 7
  • This won't work because the break must be a `TimeSpan` and not a `DateTime` and the break cannot be added it must be subtracted. – t3chb0t Dec 21 '14 at 17:49
0

Based on the screenshot you provided in the link I think you are looking for this:

DateTime entryTime = DateTime.Parse("11:30");
DateTime exitTime = DateTime.Parse("14:30");
TimeSpan breakSpan = TimeSpan.Parse("0:30");
TimeSpan workSpan = (exitTime - entryTime - breakSpan);
answerLabel.Text = workSpan.ToString();

The break must not be a DateTime but a TimeSpan.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118