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.
' 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.
Label1.Caption = 0
time1hr = d3.Hour
time1mn = d3.Minute
time2hr = d4.Hour
time2mn = d4.Minute
time1ap = Format(d3.Value, "am/pm")
time2ap = Format(d4.Value, "am/pm")
If TimeValue(d3.Value) > TimeValue(d4.Value) Then
MsgBox "Please enter the Correct In and Out Time (Morning)"
Exit Sub
End If
'''''''' HOUR CALCULATION
If Val(time1hr) < Val(time2hr) And Val(time1mn) < Val(time2mn) Then
s1 = Val(time2hr) - Val(time1hr)
ElseIf Val(time1hr) Val(time2mn) Then
s1 = (Val(time2hr) - Val(time1hr)) - 1
Else 'If Val(time1hr) > Val(time2hr) Then
s1 = (Val(time2hr)) - Val(time1hr)
'Else
' s1 = 0
End If
If (Val(time1hr) = Val(time2hr)) And time1ap = time2ap Then
s1 = 0
End If
'''''''''MIN CALCULATION
If Val(time2mn) > Val(time1mn) Then
mn = Format(Val(time2mn) - Val(time1mn), "00")
ElseIf Val(time2mn) - Val(time1mn) Then
mn = Format(Val(time1mn) - Val(time2mn), "00")
ElseIf Val(time2mn) = Val(time1mn) Then
mn = Format(0, "00")
End If
Label1.Caption = s1 & ":" & mn
Addition of two dates
tmn1 = Val(Right(Label4.Caption, 2)) + Val(Right(Label5.Caption, 2))
If tmn1 > 60 Then
Thr1 = Thr1 + 1
tmn1 = 60 - tmn1
End If
Label6.Caption = Thr1 & ":" & tmn1
Thanks, ganesh.
Congrats Masino – your formulae “Difference between two dates” was of great help to get my job done,
But I like exclude the weekends – say “Friday alone and holidays in between two dates to your first VB formulae and add that in MS Excell.
Kindly help.
Leon Jerry Mendez