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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hours and minutes vs decimals 3

Status
Not open for further replies.

33216CLC

Programmer
Aug 17, 2000
173
BS
I wish to convert a whole number and decimals to hours and minutes. Is there a simple way to do this, without having to use a function that removes the whole number? If I must remove the whole number, what is the best method to use?

Thanks
 
I'll assume that you are asking for a way to get say... 12.25 to 12 hours and 25 minutes...

The simplest way to do that would be to create a temporary variable to store the integer value in, and then subtract to get your values.

For example:
Code:
dim myDecimal, tempMinutes as double
dim hours, minutes as integer

myDecimal = 12.25

hours = cInt(myDecimal)
tempMinutes = (myDecimal - hours) * 100
minutes = cInt(tempMinutes)

You now have two integer variables, one with the hours value, and one with the minutes. Obviously, the plugging the double, tempMinutes, variable into the integer, minutes, is extraneous to the solution, but then you have two integers (which is good), then you could destroy the myDecimal and tempMinutes variables to free up memory if that's a concern.

hope it helps!:)
Paul Prewett
 
Oops! I'm sure Paul meant:
tempMinutes = (myDecimal - hours) * 60

VBA's standard data type for holding time is a Date variable. If that's what you want, and you're starting with hours < 24, just divide by 24 and convert to Date data type. For example:
hours = 12.25
time = CDate(hours / 24)

This will give a date/time value which displays as just a time, with no date. (Actually, the date part corresponds to December 30, 1899, but Access treats that as a special case meaning &quot;no date&quot; and doesn't display a date part.) Rick Sprague
 
Actually, no, I meant '*100'

If you have the left over decimal value of .25, then you would multiply that by 100 to get the desired 25...

Your way may, in fact, be better, but I did mean 100.

:)
 
Well, doesn't it REALLY need to be in the format that 33216CLC wants it in?

Just to illustrate the process, here is another version. &quot;It&quot; assummes that &quot;It&quot; is supplied with a regular datetime type variable, which for inexplicxable reasons is NOT a date, but some kind of elapsed time.

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 & &quot;Day&quot;
        If (tmpDays > 1) Then
            tmpStr = tmpStr & &quot;s&quot;
        End If
    End If

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

    'formatting for Minutes(s)
    If (tmpMins) Then
        tmpStr = tmpStr & &quot; &quot; & tmpMins & &quot;Min&quot;
        If (tmpMins > 1) Then
            tmpStr = tmpStr & &quot;s&quot;
        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
 
Paul, Rick, Michael,

All of your posts are very helpful.


Paul,
Your post will shows how to keep the format my hours so that the number behind the decimal remains as is. I need this.

Rick,
Your post shows that .25 as a decimal or fraction is actually only 15 mins, when dealing with hours and minutes. I also need this.

Michael,
Your post shows how to convert from fraction to minutes as well as formatiing for different results. I need this.

What I wanted was a way for an end user to enter the hours worked by employees in the 12.25 format which represents 12 hrs and 25 mins (instead of using 12:25). I also want the end user to have the ability to enter this time as 12.42 (which is the hours along with the decimal or fraction which represents the minutes).

So you see all of your posts were a great help.

Thanks alot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top