C# 将时间转换为十进制

C# 将时间转换为十进制,c#,asp.net,datetime,decimal,type-conversion,C#,Asp.net,Datetime,Decimal,Type Conversion,我所要做的就是找到员工的加班时间。考勤表是CSV文件,我已经将数据保存到表中。数据字段小时数为12:10:00.0000000,每天工作小时数为11:00:00.0000000。我已经使用Timespan计算了这两次之间的差异 DateTime date, hours, working_hours ; TimeSpan ot_hours = TimeSpan.Zero; TimeSpan tot_hours = TimeSpan.Zero; if (hours >

我所要做的就是找到员工的加班时间。考勤表是CSV文件,我已经将数据保存到表中。数据字段小时数为12:10:00.0000000,每天工作小时数为11:00:00.0000000。我已经使用Timespan计算了这两次之间的差异

   DateTime date, hours, working_hours ;
   TimeSpan ot_hours = TimeSpan.Zero;
   TimeSpan tot_hours = TimeSpan.Zero;
   if (hours > working_hours)
    {
       ot_hours = (hours - working_hours);
     }
此代码已设置为循环。完成循环后,我需要将总加班时间存入另一个变量。如此写道

 tot_hours += tot_hours + ot_hours;
接下来,我需要找到员工的加班工资金额。 我试着把这个tot_小时(TimeSpan)转换成十进制。但它不起作用

   tothrsvalue = Convert.ToDecimal(tot_hours);
   totalvalue = rate * tothrsvalue;
这里的任何人。。请看一看,帮帮我。。提前谢谢

总计算部分如下所示:

         decimal basics = Convert.ToDecimal(dtamt.Rows[0]["Amount"].ToString());
                            decimal rate = 0;
                            rate = ((basics / 30) / 8);
                            txtrate.Text = rate.ToString("0.000");

                            tothrsvalue = Convert.ToDecimal(total);
                            totalvalue = rate * tothrsvalue;
                            txtamount.Text = totalvalue.ToString("0.000");
                            amt = Convert.ToDecimal(txtamount.Text);

TimeSpan
解析为
decimal
没有太大意义,因为时间根本不是一个数值

但是您可以使用它,它返回
double

var total = tot_hours.TotalHours;

您需要将TimeSpan转换为十进制吗?它不能这样做,因为它不是数字

您可能需要
tot_hours.TotalHours
,它是一个包含小数部分的双倍值


tot_hours.TotalHours.ToString(#.00”)

加班时间现在是时间戳差=秒将它们除以60,即为分钟


示例:总计=2分钟=120秒等)

我也试过。。总计=总计小时数。总计小时数;好啊其他变量的类型是decimal,我期望
tot_hours+=tot_hours+ot_hours
是错误的,因为这基本上意味着将总数增加一倍,然后再加上加班时间。如果总数为
0
,则您不会注意到这是一个问题,但在其他情况下,它会导致不正确的结果。您应该只使用
tot_hours+=ot_hours或<代码>总时数=总时数+加班时数是的。。谢谢@musefan。。那是我的错误。我已经改正了