我需要帮助计算值班打卡的延迟时间,但我的代码无法工作,这实际上不是打卡——我只是举个例子
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
For Each row As DataGridViewRow In frmExcelGrid.Rows
Dim clock1 As DateTime
Dim clock2 As DateTime
Dim total As TimeSpan
clock1 = DateTime.Parse(row.Cells("clock in").Value)
clock2 = DateTime.Parse(row.Cells("clock out").Value)
total = clock1 - clock2
row.Cells("total").Value = total
Next
End Sub
图像
您可能需要从时钟输出中减去时钟输入(否则可能会出现负时间跨度)。
DataGridView单元格的值是一个对象,因此在将其解析为DateTime之前,您可能需要一个.ToString。
设置Value
时,您可能还需要.ToString()
:
Dim clockIn = DateTime.Parse(row.Cells("clock in").Value.ToString())
Dim clockOut = DateTime.Parse(row.Cells("clock out").Value.ToString())
Dim total = clockOut - clockIn
row.Cells("total").Value = total.ToString()
如果DateTime.Parse不起作用,您将不得不查看DateTime.ParseExact