tags:

views:

403

answers:

2

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

+8  A: 
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.

Brandon
I think you've reversed the two `DateTime`s; the span you'd get with this is negative.
Daniel LeCheminant
@Daniel, right you are. Fixed.
Brandon
A: 

How about this?

TimeSpan inTime = new TimeSpan(9, 35, 0); //or TimeSpan.Parse("9:35");
TimeSpan outTime = new TimeSpan(11, 55, 0); //or TimeSpan.Parse("11:55");

TimeSpan total = outTime - inTime;

decimal timevalue = total.Hours + (total.Minutes > 0 ? total.Minutes/60 : 0);

string totalString = timevalue.ToString();
Bob