【问题标题】:How to calculate total working hour in 00:00 format?如何以 00:00 格式计算总工作时间?
【发布时间】:2014-05-19 11:34:37
【问题描述】:

我有一个 gridview,它有一个名为小时的列,格式为 00:00,我想计算总小时数并显示在文本框中。

更新:示例 2:10+3:50=6:00

到目前为止我的男女同校:

Protected Sub grdWork_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.RowType = DataControlRowType.DataRow Then
        Dim txtHours As TextBox
        txtHours = DirectCast(e.Row.FindControl("txtHours"), TextBox)
        Dim Hours1 As String = txtHours.Text

        Dim Hours2 As String = "2:50"

        Dim TS1 As New TimeSpan(Integer.Parse(Hours1.Split(":"c)(0)), Integer.Parse(Hours1.Split(":"c)(1)), 0)

        If txtHours.Text <> "" Then
            'TotalChargeableValue = TotalChargeableValue + Convert.ToDouble(txtHours.Text)
            'Dim ts2 As TimeSpan



            'Dim TS2 As New TimeSpan(Integer.Parse(Hours2.Split(":"c)(0)), Integer.Parse(Hours2.Split(":"c)(1)), 0)


            ts2 = ts2.Add(TS1)
            'ts2 = TS1

            Session("timespan") = ts2

            'ts2 = ts2.Add(TS1)
            txtSum.Text = ts2.ToString()
            'MsgBox(TS3.ToString())

        End If
    End If
End Sub

【问题讨论】:

  • 您是否遇到任何错误?有什么问题?
  • 它没有计算准确的时间。@Jonny
  • 您能否举个例子说明您的值以及您的代码计算的总数。
  • 您的问题和示例代码不匹配。你说你想计算2:10+3:50,但你将值转换为一个不能采用2:10或3:50格式的双精度值。我怀疑你的格式是错误的。你能举一个例子,说明你希望在 txtHours 中找到什么,以及 TotalChargeableHours 的数据类型是什么?
  • 请查看我更新的代码。@CraigMoore

标签: asp.net vb.net gridview


【解决方案1】:
TimeSpan t1 = TimeSpan.Parse("23:30");
TimeSpan t2 = TimeSpan.Parse("00:40:00");
TimeSpan t3 = t1.Add(t2);
Console.WriteLine(t3); // 1.00:10:00

使用日期时间:

DateTime d1 = DateTime.Parse("23:30");
DateTime d2 = DateTime.Parse("00:40:00");
DateTime d3 = d1.Add(d2.TimeOfDay); 
Console.WriteLine(d3.TimeOfDay); // 00:10:00

【讨论】:

  • 请用VB,因为问这个问题的人可能不懂c#,觉得这个答案没用。
  • @Jonny 然后他们会使用像converter.telerik.com 这样的转换器正确的方向,而不是真正做他们的“功课”。
猜你喜欢
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
相关资源
最近更新 更多