How to calculate the difference between two times?

I have two text fields that allow the user to enter a start time and an end time in this format (h: mm). I want it to return the difference in the label. For example, if a user enters 1:35 in the first text field and 3:30 in the second text field and clicks the "Calculate" button, he will return the time 1:55.

Any ideas or resources for this? I just want to calculate the difference in hours and minutes between two text fields. Date and seconds do not matter.

+3
source share
2 answers

Parse both the values ​​of the text fields TimeSpan, and then you can use them using -.

TimeSpan ts1 = TimeSpan.Parse(textBox1.Text); //"1:35"
TimeSpan ts2 = TimeSpan.Parse(textBox2.Text); //"3:30"

label.Text = (ts2 - ts1).ToString();         //"1:55:00"
+7
source

DateTime, :

DateTime.Parse(startDate).Subtract(DateTime.Parse(endDate)).Duration().ToString("hh:mm");
0

All Articles