Convert Time to decimal in C#

c#, time

Solution

DateTime dt1 = DateTime.Parse("11:55");    
DateTime dt2 = DateTime.Parse("9:35");

double span = (dt1 - dt2).TotalHours;

Do you actually need the "2:20" or is that just an intermediate step?

Edit: If you wanted to go back, you'd just need to do a little bit of math. Take the remainder of the decimal and multiply by 60, then round. Those will be the minutes, so just add them to the hours.

Problem

what i need to do, is have two defined strings that is inputted by the user, ``` string in = "9:35"; //am string out = "11:55"; //am ``` and i need to subtract them, so that would get the total hours that they were signed in. which should equal: ``` string total = "2:20" ``` then, i need to convert that into a decimal.. so, 2:20 would be ``` string decimal = "2.33"; ``` I dont know how to do that, any help would be appreciated! P.S: id also like to be able to calculate the total hours they were checked in from a decimal number, so basically the opposite

Original source