Difference between two times follow up (Convert to decimal)

I asked a question like this earlier and got a great answer but I made a mistake and wanted the output to be a decimal rather than a time. So here's the question.

I have two textboxes that allow a user to enter a start time and an end time in this format (h:mm). I want it to return the difference in a label. For example, if a user enters 1:35 in the first textbox and 3:30 in the second textbox and press the 'Calculate' button, it will return the decimal 1.92.

Any ideas or resources for this? I only want to calculate decimal difference of the time entered, date and seconds doesn't matter at all. Below is the code for getting an output in the format of (h:mm).

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

label.Text = (ts2 - ts1).ToString();         //"1:55:00"
Jon Skeet
people
quotationmark

It sounds like you want the total number of hours, in that 1.92 hours is 115 minutes (ish).

In that case you want:

double hours = (ts2 - ts1).TotalHours;

... you can then format that how you wish (e.g. to 2 decimal places).

For example:

TimeSpan ts1 = TimeSpan.Parse("1:35");
TimeSpan ts2 = TimeSpan.Parse("3:30");
double hours = (ts2 - ts1).TotalHours;

Console.WriteLine(hours.ToString("f2")); // Prints 1.92

Of course I'd personally use Noda Time and parse the strings as LocalTime values instead of TimeSpan values, given that that's what they're meant to be (times of day), but that's a minor quibble ;)

people

See more on this question at Stackoverflow