如何减去和添加时差

时间:2016-01-27 13:36:21

标签: vb.net datetime ms-access time timespan

我正在完成这项任务,将获得当天晚些时候的总数。数据库后期有3列,它是lateAMlatePMtotallate。在lateAM如果用户在上午7:31参加,他会迟到1分钟。在latePM,如果他在下午1:05参加,他会迟到5分钟。然后我需要添加lateAMlatePM以获得6分钟的totallate

 这是获取lateAM

的代码
  Dim timelate As TimeSpan
    Dim time As Date = #7:30:00 AM#
    Dim zerotime As TimeSpan = TimeOfDay - TimeOfDay
    If TimeOfDay > #7:30:00 AM# Then
        timelate = TimeOfDay - time
    Else
        timelate = zerotime 'output 0:00:00
    End If

   Try
        Dim cnn As New OleDbConnection(conString)
        query = "Insert into tbldtr(EmpID,empname,timeinAM,lateam) values ('" & txtInputID.Text & "', '" & txtEmpName.Text & "','" & TimeOfDay & "','" & timelate.ToString & "')"
        cmd = New OleDbCommand(query, cnn)
        cnn.Open()
        cmd.ExecuteNonQuery()
        MessageBox.Show("You have successfully Time in AM")
        cnn.Close()

    Catch ex As Exception
        MsgBox("ERROR: " & ErrorToString(), MsgBoxStyle.Critical)
    End Try

这是获取latePM并添加lateAMlatePM

的代码
    Dim timelate As TimeSpan
    Dim time As Date = #1:00:00 PM#
    Dim zerotime As TimeSpan = TimeOfDay - TimeOfDay
    If TimeOfDay > #1:00:00 PM# Then
        timelate = TimeOfDay - time
    Else
        timelate = zerotime
    End If

          Try
        Dim cnn As New OleDbConnection(conString)
        query = "update tbldtr set timeinPM = '" & TimeOfDay & "',latepm = '" & timelate.ToString & "', totallate = lateam + latepm"
        cmd = New OleDbCommand(query, cnn)
        cnn.Open()
        cmd.ExecuteNonQuery()
        MessageBox.Show("You have successfully Time in PM")
        cnn.Close()

    Catch ex As Exception
        MsgBox("ERROR: " & ErrorToString(), MsgBoxStyle.Critical)
    End Try

我的问题是我无法添加lateAMlatePM。 我正在使用vb和msaccess作为数据库。我的lateAMlatePM列的数据类型为shorttext

1 个答案:

答案 0 :(得分:1)

您应始终将日期/时间存储为日期,以便更改字段的数据类型。

然后:

query = "Insert into tbldtr(EmpID,empname,timeinAM,lateam) values ('" & txtInputID.Text & "', '" & txtEmpName.Text & "',#" & TimeOfDay.ToString("yyyy'/'MM'/'dd") & "#,#" & timelate.ToString("yyyy'/'MM'/'dd") & "#)"

修改

要仅存储时间,请使用以下格式:

query = "Insert into tbldtr(EmpID,empname,timeinAM,lateam) values ('" & txtInputID.Text & "', '" & txtEmpName.Text & "',#" & TimeOfDay.ToString("HH':'mm':'ss") & "#,#" & timelate.ToString("HH':'mm':'ss") & "#)"