Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Time to decimal in C#

Tags:

c#

time

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

like image 303
Xavier Avatar asked Dec 02 '09 16:12

Xavier


People also ask

How do you convert seconds to HH MM SS in C?

You need h->segundos = segundos % 60; in the function.

How do I convert time to a decimal in Excel?

The easiest way to convert time to decimal in Excel is to multiply the original time value by the number of hours, seconds or minutes in a day: To convert time to a number of hours, multiply the time by 24, which is the number of hours in a day.


1 Answers

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.

like image 181
Brandon Avatar answered Sep 23 '22 10:09

Brandon