This following code will show you how to calculate difference between two dates by using Visual Basic 6. The calculation result will give you an output that contains of the difference between those two dates in format: Days, Hours:Minutes:Seconds. The both dates must be in complete format. For example: the first date is March 1, 2002 17:18:00, and the second date is September 1, 2002 09:42:30. After being calculated, then the output will give you the result as follow: 183 day(s), 16:24:30. It means: The difference between those two dates is: 183 days, 16 hours, 24 minutes, and 30 seconds.
'Description: Calculate difference between 2 particular dates ' and show the result in format: day(s), hr:min:sec ' For example: ' The first date = 01/03/2002 17:18:00 and ' The second date = 01/09/2002 09:42:30, then ' the result will output --> 183 day(s), 16:24:30 ' (183 days, 16 hours, 24 minutes, and 30 seconds). ' You can use the DateDiff function belongs to VB6 'Author : Masino Sinaga 'Created : Sunday, September 1, 2002 'Preparation: 1. Create a new standar exe project with 1 Form. ' 2. Add controls: 2 TextBoxes, 1 Label, and 1 Timer. ' 3. Copy this following code to the form editor form. '------------------------------------------------------------------- Option Explicit Function CalcDiff2Dates(ByVal d1 As Date, _ ByVal d2 As Date) As String Dim Second As Long, Day As Long, Hour As Long Dim CompleteHour As String If d1 > d2 Then MsgBox "The first date must be lower than " & _ "the second date!", _ vbCritical, "Warning" Exit Function End If 'Count the difference in seconds Second = DateDiff("s", d1, d2) 'Calculate the hour by dividing with 3600 '(backslash ("\") in order to get the Integer 'without decimal value) Hour = Second \ 3600 'If hour greater than 23 'it means: more than 1 day If Hour > 23 Then 'Calculate day by dividing with 24 '(backslash ("\") in order to get the Integer 'value without decimal value) Day = Hour \ 24 'Calculate the hour duration in hh:mm:ss CompleteHour = Format((d2 - d1), "hh:mm:ss") Else 'If the hour less than or equal with 23 Day = 0 'assign the Day with zero value 'Calculate hour duration in hh:mm:ss CompleteHour = Format((d2 - d1), "hh:mm:ss") End If If Day = 0 Then 'If less than 1 day 'Get the final result CalcDiff2Dates = CompleteHour Else 'If day greater than 0, show the day duration 'Get the final result CalcDiff2Dates = Day & " day(s), " & CompleteHour End If Exit Function End Function Private Sub Form_Load() Timer1.Interval = 500 Timer1.Enabled = True Text1.Text = "01/03/2002 17:18:00" 'Text2.Text = "01/09/2002 09:42:30" Text2.Text = Now End Sub Private Sub Timer1_Timer() On Error GoTo ErrHandler Text2.Text = Now Label1.Caption = CalcDiff2Dates(CDate(Text1.Text), _ CDate(Text2.Text)) Exit Sub ErrHandler: MsgBox "Invalid date or format!", _ vbCritical, "Error Date" End Sub
From the code above, the first parameter belongs to the CalcDiff2Dates function placed on Text1 control, whereas the second parameter placed on Text2 which its value being generated by Timer1 control using time interval one second.
The result being shown at Label1 control based the changes generated by Timer1 control. Of course, you may modify that code above by yourself. For example, by removing the Timer control and disable the Timer code, and then simply use the CalcDiff2Dates function only in Form_Load procedure.