vb.net减去日期和时间
你好,我有这么多的问题,试图得到正确的,这让我疯狂我想做的是,我有一个开始和停止按钮的形式开始和停止时间。我用excel作为我的数据库好的。我需要做的是,当你按下start时,它会将现在的时间记录到excel单元格中。我们会说A1我记录它,因为我可能会关闭程序,然后当你回到程序中,我点击停止,我需要获取开始时间,然后从现在的时间中减去它,比如 span=Datetime.Now.starttime 然后,我需要用h:mm格式的时间,因为它只需要几个小时和几分钟,然后用excel中的A2。在上面的h:mm格式中,这里还有一个问题!说我回去重新开始工作!再次让它把现在的时间放在A1,然后我去停止我的时间 如果oXL.RangeA2.Value=则 然后我需要它做它以前做过的事情,取停止时间,从开始时间中减去它,然后把它放在格式h:mm中,然后把它加到A2中的时间,这将是所有的总时间!请帮帮我,我已经做了好几个小时了。这是我真正的代码,但我已经对它做了很多修改,如果你能给我一个A1和A2的示例程序,我就可以让它为我想做的工作而工作。因为我的程序非常庞大,需要很长时间才能解释清楚。多谢各位vb.net减去日期和时间,vb.net,Vb.net,你好,我有这么多的问题,试图得到正确的,这让我疯狂我想做的是,我有一个开始和停止按钮的形式开始和停止时间。我用excel作为我的数据库好的。我需要做的是,当你按下start时,它会将现在的时间记录到excel单元格中。我们会说A1我记录它,因为我可能会关闭程序,然后当你回到程序中,我点击停止,我需要获取开始时间,然后从现在的时间中减去它,比如 span=Datetime.Now.starttime 然后,我需要用h:mm格式的时间,因为它只需要几个小时和几分钟,然后用excel中的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
您是说只需要在应用程序外部存储两个值吗?如果是这样,那么您就不需要数据库或Excel,因为它实际上不是数据库。您只需添加DateTime和TimeSpan类型的两个应用程序设置。您只需在代码中使用My.Settings的两个属性,应用程序就会自动保存和加载它们。将Option Strict置于代码文件的顶部,或在项目的属性中设置它。当您从另一个日期时间减去一个日期时间时,您将得到一个TimeSpan对象。您可以使用TimeSpan的.ToString方法来显示差异,以您想要的格式显示。要以所需格式显示时间跨度,可以尝试:oXL.ActiveCell.Offset0,18.Value=span。ToString@hh\:mm@ChrisDunaway-VB.NET不需要@