转换时间在C#为十进制 [英] Convert Time to decimal in C#

查看:406
本文介绍了转换时间在C#为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的,是有一个由用户输入的两个定义的字符串,

 在=字符串9 :35; //上午
串出=11:55; //上午

和我需要减去他们,这样会得到总时数,他们签署其中应该等于:

 字符串总=2:20

然后,我需要的转换为十进制..所以,2:20就

 字符串小数=2.33; 



我不知道该怎么做,任何帮助,将不胜感激!



PS:ID也希望能够来计算,他们从一个十进制数被检查的总时间,所以基本上相反


解决方案

 的DateTime DT1 = DateTime.Parse(11:55); 
日期时间DT2 = DateTime.Parse(9:35);

双跨=(DT1 - DT2).TotalHours;



你实际需要的2:20或者是只是一个中间步骤?



编辑:如果你想回去,你只需要做的数学一点点。取小数剩余时间和乘以60,那么圆。这些将是分钟,所以只需将它们添加到小时。


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

解决方案

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.

这篇关于转换时间在C#为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆