How to compute the duration of time

vb6 Guam
  • 14 years ago

    hello please help me anyone the problem is i know how to copute for the duartion of 2 time like for example a login and a log out and there duration but i dont know how to compute if the log out is more than 1 day and week

    please give me the code for computing duration for more than 1 day or 1 week here is the sample code

    Dim Time1 As Date

    Dim Time2 As Date

    Dim Dur As Date

    get the time first to be stored in text1 and text2

    Time1 = CDate(Text1.Text)

    Time2 = CDate(Text2.Text)

    Dur = Time2 - Time1

    Text3.Text = Format(Dur,"hh:mm:ss")

    this code computes duration for just 1 day and i want to know how to compute for more than a day or a week please help me please......

  • 14 years ago

    Hi Niko
     I herewith post coding for duration calculation.
     I hope this will helpful for u

     u just open one VB-Form add three TextBox text1,text2,text3 and add one more command button name as command1.

    Finally copy and paste the follwoing code
    Run the form and click the command button no need to give input.
    This is for Example i am posting here

    Private Sub Command1_Click()
    Dim Dt1, Dt2 As Date
    Dim strHour As String
    Dim strMin As String
    Dim strSec As String


    Text1.Text = "09-Jun-2006 05:00:10"
    Text2.Text = "13-Jun-2006 23:56:59"
    Dt1 = CDate(Text1.Text)
    Dt2 = CDate(Text2.Text)
    strSec = DateDiff("s", Dt1, Dt2)

    If CLng(strSec) > 0 Then
        strHour = Int(CLng(strSec) / 3600)
        strSec = CLng(strSec) Mod 3600
        If CLng(strSec) > 0 Then
            strMin = Int(CLng(strSec) / 60)
            strSec = CLng(strSec) Mod 60
        End If
    End If

    If Len(strHour) = 1 Then
     strHour = "0" & strHour
    End If

    If Len(strMin) = 1 Then
     strMin = "0" & strMin
    End If

    If Len(strSec) = 1 Then
     strSec = "0" & strSec
    End If

    Text3.Text = strHour & ":" & strMin & ":" & strSec
    End Sub

    Regards
    Hari K



















































  • 14 years ago

    In the example given, the output would be: 114:56:49.  There are two ways in which you can extend Hari's code to break down the output into days and hours.

    Get answer as whole number of hours and divide by 24 hours in a day:
    This is an example of how you can calculate the number of days in a given number of hours by dividing the number of hours by 24, just take out the line that reads Text3.Text = strHour & ":" & strMin & ":" & strSec and cut and paste the following in its place:

    Dim counter As Integer
    Dim whole As Integer
    Dim remainder As Integer
    whole = 0
    If strHour > 24 Then
        For counter = strHour - 24 To 0 Step -24
            whole = whole + 1
            If counter < 24 Then
                remainder = counter
                counter = counter - remainder
            End If
        Next
    Else
       remainder = strHour
    End If
    If whole > 0 Then
        Text3.Text = whole & "Days " & remainder & "Hours " & strMin & "Mins " & strSec & "Secs"
    Else
        Text3.Text = remainder & "Hours " & strMin & "Mins " & strSec & "Secs"
    End If

    Calculate number of whole days between the dates, then add this number to date1:
    This is a bit slicker, just calculate the difference between the two dates using "d" as the interval, this will give you a whole number of days.  Once you know the number of days you can add this to date1 and perform the datediff calculation again to find the number of hours:

    Private Sub Command1_Click()
    Dim Dt1, Dt2 As Date
    Dim strHour As String
    Dim strMin As String
    Dim strSec As String
    Dim nofdays As String
    Text1.Text = "09-Jun-2006 05:00:10"
    Text2.Text = "13-Jun-2006 23:56:59"
    Dt1 = CDate(Text1.Text)
    Dt2 = CDate(Text2.Text)

    'Calculation 1
    noofdays = DateDiff("d", Dt1, Dt2)

    'Take number of days off
    Dt1 = DateAdd("d", noofdays, Dt1)

    'Now do second calculation
    strSec = DateDiff("s", Dt1, Dt2)
    If CLng(strSec) > 0 Then
        strHour = Int(CLng(strSec) / 3600)
        strSec = CLng(strSec) Mod 3600
        If CLng(strSec) > 0 Then
            strMin = Int(CLng(strSec) / 60)
            strSec = CLng(strSec) Mod 60
        End If
    End If
    If Len(strHour) = 1 Then
     strHour = "0" & strHour
    End If
    If Len(strMin) = 1 Then
     strMin = "0" & strMin
    End If
    If Len(strSec) = 1 Then
     strSec = "0" & strSec
    End If

    Text3.Text = noofdays & " " & strHour & ":" & strMin & ":" & strSec
    End Sub


































































  • 14 years ago

    thanks to all of you guys you saved my life the 2 of you i will never forget the two of you

    thank you very much.....

  • 14 years ago

    To use less code, you can also use the following:

    iDays = CInt(Val(strHour)/24)
    iHours = Val(strHour) Mod 24


     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”