hi all!
I have created two datetime picker for getting input for start time and end time.
but i could'nt able to calculate time duration between them.

How to calculate the time duration by getting start and end time input.

Thanks in advance
elanch

Dani AI

Generated

If you only need a time-of-day duration, keep both DTPickers in Time mode and work with their Date values (which include a time). A simple approach is to compute seconds, fix overnight, then format HH:mm. This avoids pitfalls with 12/24-hour formatting and does not rely on strings.

Private Function DurationHHMM(startT As Date, endT As Date) As String
    Dim secs As Long
    secs = DateDiff("s", startT, endT)      ' seconds between the two times
    If secs < 0 Then secs = secs + 86400    ' crossed midnight
    DurationHHMM = Format$(secs \ 3600, "00") & ":" & _
                   Format$((secs \ 60) Mod 60, "00")
End Function

re: 24-hour display, in the VB6 MSComCtl2.DTPicker set a custom format:

With DTPicker1
    .Format = dtpCustom
    .CustomFormat = "HH:mm"   ' 24-hour
    ' use "hh:mm tt" for 12-hour with AM/PM
End With

if you want calendar years, months, and days (e.g., 0 years, 1 months, 3 days), compute it stepwise so month lengths are correct. Optionally add 1 to days for inclusive counting.

Private Function YMDDiff(d1 As Date, d2 As Date, Optional Inclusive As Boolean) As String
    Dim y As Long, m As Long, d As Long, t As Date
    If d2 < d1 Then t = d1: d1 = d2: d2 = t
    y = DateDiff("yyyy", d1, d2): If DateAdd("yyyy", y, d1) > d2 Then y = y - 1
    t = DateAdd("yyyy", y, d1)
    m = DateDiff("m", t, d2): If DateAdd("m", m, t) > d2 Then m = m - 1
    d = DateDiff("d", DateAdd("m", m, t), d2)
    If Inclusive Then d = d + 1
    YMDDiff = y & " years, " & m & " months, " & d & " days"
End Function

Tip: ’s idea using DateDiff is spot on; just prefer working with Date values (not strings) and normalize when the end time rolls past midnight.

Recommended Answers

All 5 Replies

U can compare dates in terms of days , months or years as u wish by DateDiff Function in VB6.
As DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
Where interval will be in string as :
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second
It will return difference of two dates as long according to parameters provided.

following codes is an example :

Private Sub btnCalculate_Click()
Dim Temp, thn, bln As Long
Temp = DateDiff("m", DTPStart.Value, DTPEnd.Value)
thn = Temp \ 12   ' Year
bln = Temp Mod 12 ' Month
MsgBox thn & " Years " & bln & " Months"
End Sub

e.g :
Start = 5/24/2008
End = 7/24/2008
Result = 0 Years 2 Months

U can compare dates in terms of days , months or years as u wish by DateDiff Function in VB6.
As DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
Where interval will be in string as :
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second
It will return difference of two dates as long according to parameters provided.

following codes is an example :

Private Sub btnCalculate_Click()
Dim Temp, thn, bln As Long
Temp = DateDiff("m", DTPStart.Value, DTPEnd.Value)
thn = Temp \ 12   ' Year
bln = Temp Mod 12 ' Month
MsgBox thn & " Years " & bln & " Months"
End Sub

e.g :
Start = 5/24/2008
End = 7/24/2008
Result = 0 Years 2 Months

Thank u 4 replying
sory i could not understand

i just need to calculate time duration not the diff. between dates.

Getting input

Start time: 05:40
End time: 06:40

output to be calculated

Time duration = 01:10

kindly help.

Private Function TimeDiff(Time1 As String, Time2 As String) As String
Dim MinsDiff As String
Dim TheHours As String
MinsDiff = DateDiff("n", Time1, Time2)
'If midnight is between times
MinsDiff = IIf(MinsDiff < 0, MinsDiff + 1440, MinsDiff)
TheHours = Format(Int(MinsDiff / 60), "00")
MinsDiff = Format(MinsDiff Mod 60, "00")
TimeDiff = TheHours & ":" & MinsDiff
End Function


Private Sub Command1_Click()
Dim x As String
x = TimeDiff(DTPicker1.Value, DTPicker2.Value)
MsgBox x
End Sub
commented: Great code +1

thank u.
problem 4 me is solved.

In date time picker AM/PM option is there but 24 Hrs format is not there. Is there any option to change the format.(I have used custom format)

I need Including Days
from = 21-feb-2013
to=23-mar-2013

want result=0 years, 1 monts, 3 days

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.