please help with time calculation

Jon123

Registered User.
Local time
Today, 06:15
Joined
Aug 29, 2003
Messages
668
I have this code
Code:
[Forms]![frm-RPlusC]![Qualtime] = Hours & " Hrs " & Minutes & " Min "
Code works fine but it displays the hours "Hrs" and Minutes "Min" so example 12 Hrs 30 Min how can I change it to give me 12.5

jon
 
Where do you calculate the minutes?

Perhaps it can be as simple as doing Minutes/60
??
 
I wish it would be nice. with Minutes/60 I get a 0 value with Minutes \60 I get 0 for the value
 
Where do you define (DIM) your MInutes variable? Is it an integer by chance?
Where to you calculate it?
How do you calculate it?
what is its origin?
 
Code:
   Dim days As Long, Hours As Long, Minutes As Long
   days = Int(CSng(interval))
   totalhours = Int(CSng(interval * 24))
   totalminutes = Int(CSng(interval * 1440))
   Hours = totalhours Mod 24
   Minutes = totalminutes Mod 60

Here you go

jon
 
I should be able to use this right.
Code:
Public Function differenceInHHMM(ByVal dihStart As Variant, ByVal dihEnd As Variant)
Dim varTemp As Variant, lngHours As Long, lngMinutes As Long
  
   varTemp = ""
  ' If either of the parameters is empty / missing jump to the end
  If Len(dihStart & vbNullString) = 0 Then GoTo differenceInHHMM_End
  If Len(dihEnd & vbNullString) = 0 Then GoTo differenceInHHMM_End
    
  ' Find difference between Start and End date/times in minutes.
  varTemp = DateDiff("n", dihStart, dihEnd)
  
  ' lngHours is the integer of the difference in varTemp divided by 60.
  lngHours = Int(varTemp / 60)
  
  ' lngMinutes is the remainder of the difference in varTemp - 60 * lngHours.
  lngMinutes = varTemp - (lngHours * 60)
  
  ' Form the output as required
    varTemp = lngHours & ":" & Format(lngMinutes, "00")
'  varTemp = lngHours & "hrs " & Format(lngMinutes, "00") & " mins"
'  varTemp = lngHours & ":" & Format(lngMinutes, "00")
  
differenceInHHMM_End:
  differenceInHHMM = varTemp
End Function
I have 3 fields [time1] [time2] which has the Startdate and time and the end date and time. but datedifference field
which has the control source set to =differenceInHHHMM([Time2],[Time1])

always shows #Name?


jon
 
Try this :), I am assuming Interval is a " proper date/time " field
Code:
   Dim days As Long, Hours As Long, Minutes As[COLOR="Red"] Double[/COLOR]
[COLOR="red"]   Hours = hour(Interval)
   Minutes = Minute(Interval) / 60[/COLOR]

If you simply want the decimal time try: (Now() - Date()) * 24
That should give you the decimal time... like 8.18 at the time of posting 8:11 am
if you want to split that into something like 8 Hours 18 mins, that is "kinda" nonsence

If you want 8:11 am as 8 hours 11 minutes, try using: Format((now() - date()), "HH ""Hour"" MM ""Mins""")

No need for complicated functions just a simple inline calculation or format of a calculation.

Edit: Your function seems to work just fine for me, returning 8:11... which is exactly the same as " simply" doing:
Format((now() - date()), "HH:MM")
which seems a lot easier to do than to make your function
 
minutes/60 ought to work.

maybe format(minutes/60,"0.00")

----
try this little function



Code:
[B]Function showdecimaltime(mytime As Date) As Double[/B]
[B]       showdecimaltime = DatePart("h", mytime) + DatePart("n", mytime) / 60[/B]
[B]End Function[/B]
 
Sub testit()
Dim t As Date
t = #12:30:00 PM#
MsgBox (showdecimaltime(t))
End Sub
 
Last edited:
Guys Im getting really confused.

May we please back up just a tad.

On my form I have 2 fields that are formatted to General Date
[txtTime1] and [txtTime2]

now I have a 3 field [txtdifference] which has nothing on the format line.
The control source I have set Format((txtTime2() - txtTime1()), "HH:MM")
the value in the field is #Name?
Should this work? Again all I want is the total hrs/minutes between to Dates.

jon
 
no that wont work, you dont reference fields using the ()

Try using the expresion builder to find out how you should reference them.
 
I believe that the control source would be

Code:
[B]=[/B]Format(([txtTime2] - [txtTime1]), "HH:MM")

If you wanted like XXX.Y Hrs you could try ...

Code:
=(Datediff("n", [txtTime2], [txtTime1]) / 60)

If that doesn't work you might need to wrap it in a function.

Code:
Public Function diffInHH(byVal dihTimeEnd as Variant, byVal dihTimeStart as Variant)
Dim varTemp as Variant

  varTemp = (Datediff("n", dihTimeEnd, dihTimeStart) / 60)
  
  diffInHH = varTemp
End Function

And use the Control Source

Code:
=diffInHH([txtTime2], [txtTime1])

Did you have that original function in post 6, that gave #Name, in the form code or in a module? It might only work if in a module rather than the form code.


Edit: Your function seems to work just fine for me, returning 8:11... which is exactly the same as " simply" doing:
Format((now() - date()), "HH:MM")
which seems a lot easier to do than to make your function

The function differenceInHHHMM(), which looked rather familiar as I looked at it, was written to display intervals where the number of hours was greater than 24.
 
Last edited:
Yes access STILL does not have a proper way of dealing with periods greater than 24 hours in a time format :/

Excel does, but access doesnt *GRMBL*
 

Users who are viewing this thread

Back
Top Bottom