vb.net减去日期和时间

时间:2014-09-19 05:16:15

标签: vb.net

你好我在尝试做到这一点时遇到了很多问题而且它让我感到疯狂,我想要做的是我在开始和停止时间的表格中有一个开始和停止按钮。我正在使用excel作为我的数据库。我需要做的是当你按下开始时它将Now Time记录到excel单元格。我们会说A1我记录它因为我可以关闭程序然后当你回到程序时我点击停止我需要抓住那个开始时间然后把它从现在的时间减去它,如

  

span = Datetime.Now.Subtract(StartTime)

然后我需要花费那个应该只有h:mm格式的时间,因为它只会是几小时和几分钟,并把它放在excel中说A2。采用上述格式" h:mm"现在这是另一个问题!说我回去再开始工作!再次让它把现在的时间放在A1,然后我就停止了我的时间

  

如果是oXL.Range(" A2")。值=""然后

然后我需要它做它之前做的事情并采取停止时间并从开始时间减去它并将其格式化为h:mm而不是将其添加到A2中的时间,这将超过所有总时间!请帮助我,我已经工作了几个小时。这是我真正的代码,但我已经改变了它,如果你能给我样本A1和A2程序,我会更好,我可以让我的工作做我想做的事情。因为我的计划很庞大,需要很长时间来解释一切。谢谢。

  Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        Dim Found As Excel.Range
        Dim EndDate As String = DateTime.Now.ToString("MM-dd-yyyy")
        Dim EndTime As String = DateTime.Now
        Dim TotalTime As String
        Dim CompleteTime As TimeSpan
        Dim result As String

        strJob = InputBox("Enter Job Number")
        strInitials = InputBox("Enter Initials.")


        If strJob = "" Then
            MsgBox("You have to enter a Job Number.")
            Exit Sub
        End If

        If strInitials = "" Then
            MsgBox("You have to enter your Initials.")
            Exit Sub
        End If

        Found = oSheet.Cells.Find(strJob)

        If Found Is Nothing Then
            MsgBox("Job was not found.")

        Else
            Found.Activate()

            'Check for Programmer one.
            If oXL.ActiveCell.Offset(0, 2).Value = "" Then
                MsgBox("This job has not been started")
                Exit Sub

            ElseIf oXL.ActiveCell.Offset(0, 2).Value = strInitials Then
                If oXL.ActiveCell.Offset(0, 13).Value = Nothing Then
                    MsgBox("You have not started this job yet")
                    Exit Sub
                End If

                Dim time1 As DateTime = oXL.ActiveCell.Offset(0, 13).Value
                If oXL.ActiveCell.Offset(0, 7).Value = Nothing Then
                    Dim span As TimeSpan
                    span = DateTime.Now.Subtract(time1)
                    TotalTime = (span.Days * 24) + span.Hours & ":" & (span.Minutes)
                    oXL.ActiveCell.Offset(0, 7).Value = TotalTime
                Else

                    Dim time3 = TimeSpan.ParseExact(oXL.ActiveCell.Offset(0, 7).Value, "h\:mm", CultureInfo.CurrentCulture)
                    Dim span As TimeSpan
                    span = DateTime.Now.Subtract(time1)
                    TotalTime = span.Hours & ":" & (span.Minutes)
                    oXL.ActiveCell.Offset(0, 14).Value = TotalTime
                    Dim time2 = TimeSpan.ParseExact(oXL.ActiveCell.Offset(0, 114).Value, "h\:mm", CultureInfo.CurrentCulture)
                    CompleteTime = time2 + time3
                    oXL.ActiveCell.Offset(0, 7).Value = CompleteTime
                End If
                oXL.ActiveCell.Offset(0, 13).Value = ""
                oXL.ActiveCell.Offset(0, 14).Value = ""


                result = MessageBox.Show("Is this job complete?", "Complete", MessageBoxButtons.YesNo)
                If result = DialogResult.No Then

                ElseIf result = DialogResult.Yes Then
                    oXL.ActiveCell.Offset(0, 4).Value = EndDate
                End If

                'Check for programmer two.
            ElseIf oXL.ActiveCell.Offset(0, 10).Value = "" Then
                MsgBox("You Are not working on this job")
                Exit Sub
            ElseIf oXL.ActiveCell.Offset(0, 10).Value = strInitials Then

                If oXL.ActiveCell.Offset(0, 17).Value = Nothing Then
                    MsgBox("You have not started this job yet")
                    Exit Sub
                End If

                Dim time1 As DateTime = oXL.ActiveCell.Offset(0, 17).Value
                'Dim time1 = TimeSpan.ParseExact(oXL.ActiveCell.Offset(0, 17).Value, "h\:mm", CultureInfo.CurrentCulture)
                'Dim time2 = TimeSpan.ParseExact(oXL.ActiveCell.Offset(0, 18).Value, "h\:mm", CultureInfo.CurrentCulture)
                If oXL.ActiveCell.Offset(0, 12).Value = Nothing Then
                    Dim span As TimeSpan
                    span = DateTime.Now.Subtract(time1)
                    TotalTime = (span.Days * 24) + span.Hours & ":" & (span.Minutes)
                    oXL.ActiveCell.Offset(0, 12).Value = TotalTime

                Else
                    Dim time2 = TimeSpan.ParseExact(oXL.ActiveCell.Offset(0, 12).Value, "h\:mm", CultureInfo.CurrentCulture)

                    Dim span As TimeSpan
                    span = DateTime.Now.Subtract(time1)
                    TotalTime = span.Hours & ":" & (span.Minutes)
                    oXL.ActiveCell.Offset(0, 18).Value = TotalTime
                    Dim time3 = TimeSpan.ParseExact(oXL.ActiveCell.Offset(0, 18).Value, "h\:mm", CultureInfo.CurrentCulture)
                    CompleteTime = time2 + time3
                    oXL.ActiveCell.Offset(0, 12).Value = CompleteTime
                End If
                oXL.ActiveCell.Offset(0, 17).Value = ""
                oXL.ActiveCell.Offset(0, 18).Value = ""
                oXL.ActiveCell.Offset(0, 12).Value = CompleteTime.ToString("h\:mm")


                result = MessageBox.Show("Is this job complete?", "Complete", MessageBoxButtons.YesNo)
                If result = DialogResult.No Then

                ElseIf result = DialogResult.Yes Then
                    oXL.ActiveCell.Offset(0, 4).Value = EndDate
                End If

            Else
                MsgBox("You Are not working on this job")
                Exit Sub

            End If

        End If

        'Logging
        If dicLogs & ".txt" = "" Then
            Dim LogBook = File.Create(dicLogs & ".txt")
            Dim logwriter As New System.IO.StreamWriter(LogBook)
            logwriter.Write(user & " Stopped " & strJob)
            logwriter.Close()
        Else
            Dim logwriter As New System.IO.StreamWriter(dicLogs & ".txt", True)
            logwriter.Write(vbNewLine & user & " Stopped " & strJob)
            logwriter.Close()

        End If

        save()
    End Sub

0 个答案:

没有答案