Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating duration in hours and minutes

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Does anyone know how to calculate the duration between two times with the output given in hours and minutes? Using DateDiff with the "h" operator, I can get Access to give me the duration in hours (as an integer), or using "n", in minutes, but I want it to say 1:15 if it's an hour and 15 minutes.
 
Just get the value in minutes, and then run the following code
Code:
'Here, we assume you already have a minutes variable
dim hours
hours = minutes mod 60
minutes = minutes - (hours * 60)

Then, just output the two variables with a colon in between...

hope it helps! :)
Paul Prewett
 
Paul Prewett's soloution is somewhat incomplete. It responsd DIRECTLY to the example given, but ignores any possability that the interval might be greater than 24 Hrs, when MOST would prefer the results to expressed in Day(s), Hr:MM


Code:
Public Function basDbl2HrsMin(AccumTime As Double) As String

    Dim tmpDays As Double
    Dim tmpHrs As Double
    Dim tmpMins As Double
    Dim tmpTime As Double
    Dim tmpStr As String

    tmpDays = Int(AccumTime)
    tmpTime = (AccumTime - tmpDays) * 1440      'Fraction to Mins

    tmpMins = tmpTime Mod 60
    tmpTime = tmpTime - Int(tmpMins)
    tmpHrs = tmpTime \ 60

    'formatting for Day(s)
    If (tmpDays) Then
        tmpStr = tmpDays & "Day"
        If (tmpDays > 1) Then
            tmpStr = tmpStr & "s"
        End If
    End If

    'formatting for Hour(s)
    If (tmpHrs) Then
        tmpStr = tmpStr & " " & tmpHrs & "Hr"
        If (tmpHrs > 1) Then
            tmpStr = tmpStr & "s"
        End If
    End If

    'formatting for Minutes(s)
    If (tmpMins) Then
        tmpStr = tmpStr & " " & tmpMins & "Min"
        If (tmpMins > 1) Then
            tmpStr = tmpStr & "s"
        End If
    End If


    basDbl2HrsMin = tmpStr
    

End Function



MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
You talked about DateDiff, so I'm assuming your values are in Date variables (or DateTime fields).

First, calculate the difference and save it in a Date variable:
Dim duration As Date
duration = stoptime - starttime

If you want to round to the nearest minute, use
duration = stoptime - starttime + 3.47222222222222E-4
The constant is equivalent to 30 seconds expressed as a fraction of a day.

If the difference is limited to less than a day, the most efficient way is to simply format the difference:
something = Format(duration, "Short Time")
This gives you a string you can put into a text box control, for example.

If you want to store the result, just save the Date variable (duration). When you're ready to display it in a datasheet, form, or report, set the Format function to "Short Time".

If the difference might be a day or more, but you still want it in hours and minutes, you can't use the Format function. Instead, use
hours = Int(duration * 24#)
minutes = ((duration * 24#) - hours) * 60
result = CStr(hours) & ":" & Format(minutes, "00") Rick Sprague
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top