time conversion remake

hair

Registered User.
Local time
Today, 19:17
Joined
Mar 27, 2003
Messages
125
There is this function :

Function TimeConversion(ByVal tempTime As Date) As String

'On Error GoTo Err_Error

' This routine is given values from the calling procedure and calculates the time
' beyond the obstacle of a 24-hour clock

Dim lngDays As Long, lngHours As Long, lngMinutes As Long, lngSeconds As Long

lngDays = Int(CSng(tempTime))
lngDays = lngDays * 24
lngHours = Int(CSng(tempTime * 24))
lngMinutes = Int(CSng(tempTime * 1440))
lngSeconds = Int(CSng(tempTime * 86400))
lngHours = lngDays + (lngHours Mod 24) ' calculate hours
lngMinutes = lngMinutes Mod 60
lngSeconds = lngSeconds Mod 60

TimeConversion = lngHours & ":" & IIf(lngMinutes <= 9, "0" & lngMinutes, lngMinutes) _
& ":" & IIf(lngSeconds <= 9, "0" & lngSeconds, lngSeconds)


Exit_Error:
Exit Function

Err_Error:
MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
Resume Exit_Error

End Function

QUESTION: Is there a function that do the exact opposite thing?
Thank you
 
Try this:

Code:
Function ConvertToTime(MyTime As String) As Date
    ConvertToTime = Int(CDbl(Left(MyTime, 2)) / 24) + CDate(CInt(Left(MyTime, 2)) Mod 24 & Mid(MyTime, 3))
End Function

It will work for any string as long as its a REAL time notation and starts with 2 didgets for the hours....

Regards
 
Thank you Mailman

My life it would have been better if we had an hour format over 24h limit for calculations. In vb is the same?
 
I know its kinda late, but...

Actualy your function is not made for hour format, but for days. I can't pass it a value like 45:33 (which I want)

So again, and I have searched this forum for over two hours without succes, I need a function that does the opposite of TimeFormat showed upper.

Like you pass a string 14:40 or 176:55 and it converts it in 1,89798789 or something like this

Thank you for any idea
 
You might want to take a look at this previous post http://www.access-programmers.co.uk/forums/showthread.php?s=&postid=225183

Mailman's function is formatted to return a date. And it does work with hours over 24.
Call it from the debug window with:
? converttotime("45:33")
...and it returns
12/31/1899 9:33:00 PM
which represents 12/30/1899 + 45 hrs. and 33 minutes.

Change the declaration to:
Function ConvertToTime(MyTime As String) As Double
and call it from the debug window:
? converttotime("45:33")
...and it returns
1.89791666666667

To get a little clearer picture, try this from the debug window:

myDate = #12/31/1899 9:33:00 PM#
? cdbl(myDate)
1.89791666666667

For an indepth discussion on how Access stores date/time take a look at this link:
support.microsoft.com/support/kb/articles/Q130/5/14.ASP
 
Last edited:
thanks raskew

It works like this

..maybe Im wrong, but it appears to work with xx:xx format
If its x:xx -(like 9:00) or xxx:xx (like 123:30) it ain't
Which is normal, but I cant get the right solution to modify so that accepts 1 or 3 or 4 caracthers before :

( that I need )

Sorry for bothering ya

PS the link u gave ain't work neither
 
Last edited:
Sorry about that-

The links are fixed.

Really like Mailman's one-liner but see that it does have a problem if the input is not "##:##". Don't have time to play with it at the moment--perhaps Mailman would provide a revision.

In the interim, you could use the following:
Code:
Function tottime2(pStrDT As String) As Date
Dim numDays As Integer, numHrs As Integer, numMins As Integer, minHold As Double
Dim strHrs As String, strMins As String

numDays = Int(Val(Left(pStrDT, InStr(pStrDT, ":") - 1)) / 24)
numHrs = Int(Val(Left(pStrDT, InStr(pStrDT, ":") - 1)) Mod 24)
numMins = Val(Mid(pStrDT, InStr(pStrDT, ":") + 1))
minHold = ((60 * numHrs) + numMins) / 1440
Debug.Print numDays + minHold
tottime2 = numDays + minHold
End Function

Best wishes - Bob
 

Users who are viewing this thread

Back
Top Bottom