Full Version: Time: Minutes To Hours
UtterAccess Discussion Forums > Microsoft® Access > Access Queries
JBindus
I'm having a problem that I know I saw answered here before but for the life of me can not find the solution now. I know it can't be that difficult but I am STUMPED!

How do you convert Minutes into Hours and Minutes ? Example: A movie is 114 minutes long in a number field, I would like the query to spit out 1.54 or 1:54 to represent 1 hour, 54 minutes. I can't seem to figure this out!

Any help would be appreciated.

Jeff
mishej
I would do it like this:
CODE
Public Function displayFormattedTime(nMinutes As Long) As String

  ' get hours, append with colon and then get a zero-formatted minute number as a string
  displayFormattedTime = (nMinutes \ 60) & ":" & Format(nMinutes Mod 60, "00")

End Function

and you can test from the Immediate window (Ctrl-G):

? displayFormattedTime(114)
1:54

? displayFormattedTime(62)
1:02

JBindus
Thank you very much for your reply!

I think it's close but I found a problem.

When I enter 114 it returns 1.9:54
When I enter 125 it returns 2.0833333333:05

Is there a quick fix to this?

Again, thank you!

Jeff
mishej
Make sure you used a backslash and not a forward slash when dividing minutes by 60:

displayFormattedTime = (nMinutes 60) & ":" & Format(nMinutes Mod 60, "00")

The backslash division returns an integer values where the forward slash returns a float.
JBindus
That was it!!! It works perfectly now!!!

I never knew the difference between the slashes...

Thank you very much,

Have a great night or morning frown.gif

Jeff
mishej
Glad I could assist. Yes, it is a fiine distinction but a very important one!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.