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

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.