Solved Working with time: Showing elapsed time as base 10 number (1 Viewer)

KitaYama

Well-known member
Local time
Today, 16:29
Joined
Jan 6, 2022
Messages
1,541
It's an extended question to this post. My question is a bit different, so I started a new thread.
It also may be more a math question than an Access one, but since I don't know the correct terminology, Google couldn't help.

I have a text box that shows a calculated elapsed time between two given date/time values.
example : 2 18:30:27 (2 days, 18 hours, 30 min & 27 sec)

How can I show this value as a 10 base number?

Some examples:
1 1:30:00 --> 25.5
0 1:15:00 --> 1.25

Thanks for any kind of advice
 
Last edited:

plog

Banishment Pending
Local time
Today, 02:29
Joined
May 11, 2011
Messages
11,648
I have a text box that shows a calculated elapsed time between two given date/time values.

Some examples:
1 1:30:00 --> 25.5
0 1:15:00 --> 1.25

Just to restate concisely--You want to display the difference between two datetimes in decimal hour units.

What you need to do is use DateDiff (https://www.techonthenet.com/access/functions/date/datediff.php) to get the difference between your datetimes in seconds, then divide by 3600 to convert seconds to hours.
 

KitaYama

Well-known member
Local time
Today, 16:29
Joined
Jan 6, 2022
Messages
1,541
Just to restate concisely--You want to display the difference between two datetimes in decimal hour units.

What you need to do is use DateDiff (https://www.techonthenet.com/access/functions/date/datediff.php) to get the difference between your datetimes in seconds, then divide by 3600 to convert seconds to hours.
@plog I'm terribley sorry. I should have explained better. The elapsed time is a calculated time between two given date.
There are a lot of other intervals in between that effect the result. Machine preparations, trouble shootings, breaks, etc.

So I can not simply use Datediff between start & end time. I have to work on the return value of the function which is something like:
2 20:30:17

Any further advice is much appreciated.
 

Eugene-LS

Registered User.
Local time
Today, 10:29
Joined
Dec 7, 2018
Messages
481
How can I show this value as a 10 base number?
Code:
Private Function TimeTo10Base(vVal) As Currency
'
'---------------------------------------------------------------------------------------------------
Dim sVal$, iPos%, iVal%, dTime As Date
On Error GoTo TimeTo10Base_Err
  
    sVal = vVal & ""
    iPos = InStr(1, sVal, " ")
    TimeTo10Base = Val(Mid(sVal, 1, iPos - 1)) * 24
    dTime = TimeValue(Mid(sVal, iPos + 1))

    iVal = DateDiff("n", TimeSerial(0, 0, 0), dTime)
    TimeTo10Base = TimeTo10Base + Round(iVal / 60, 2)

TimeTo10Base_End:
    Exit Function

TimeTo10Base_Err:
    TimeTo10Base = -1
    'MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Function : " & _
           "TimeTo10Base", vbCritical, "Error!"
    'Debug.Print "TimeTo10Base_Line: " & Erl & "."
    Err.Clear
    Resume TimeTo10Base_End
End Function

Test:
Code:
    Debug.Print TimeTo10Base("1 1:30:00")
    Debug.Print TimeTo10Base("0 1:15:00")
Returns:
Code:
25,5
1,25
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:29
Joined
Feb 19, 2002
Messages
43,302
I don't understand your explanation. If you want to work with hours, then use date diff to give you a return in minutes. Then divide minutes by 60. That will give you a base 10 result in hours. Hours is the whole number and minutes is in fraction of an hour rather than minutes, so 45 minutes as the remainder = .75
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:29
Joined
Feb 28, 2001
Messages
27,194
Just to clarify for our members who didn't see (or didn't remember) the previous question to which KitaYama referred... the time in question is not the difference between two dates, but the sum of several smaller but non-contiguous (or not necessarily contiguous) intervals.
 

KitaYama

Well-known member
Local time
Today, 16:29
Joined
Jan 6, 2022
Messages
1,541
Code:
Private Function TimeTo10Base(vVal) As Currency
'
'---------------------------------------------------------------------------------------------------
Dim sVal$, iPos%, iVal%, dTime As Date
On Error GoTo TimeTo10Base_Err
 
    sVal = vVal & ""
    iPos = InStr(1, sVal, " ")
    TimeTo10Base = Val(Mid(sVal, 1, iPos - 1)) * 24
    dTime = TimeValue(Mid(sVal, iPos + 1))

    iVal = DateDiff("n", TimeSerial(0, 0, 0), dTime)
    TimeTo10Base = TimeTo10Base + Round(iVal / 60, 2)

TimeTo10Base_End:
    Exit Function

TimeTo10Base_Err:
    TimeTo10Base = -1
    'MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Function : " & _
           "TimeTo10Base", vbCritical, "Error!"
    'Debug.Print "TimeTo10Base_Line: " & Erl & "."
    Err.Clear
    Resume TimeTo10Base_End
End Function

Test:
Code:
    Debug.Print TimeTo10Base("1 1:30:00")
    Debug.Print TimeTo10Base("0 1:15:00")
Returns:
Code:
25,5
1,25
@Eugene-LS The result is not what I need. Per my example above,
1 1:30:00 --> 25.5 (1 day =24h then 24+1= 25 hours)

I didn't expect a user defined function to pass a date as string and split it and work on different parts seperately.
I hoped some vba internal functions can do the magic.
If it's the only way, I will work on user defined function to get the result.

Thanks for your time.


@Pat Hartman I'm not in a situation to explain it. I will get back to you as soon as I can.
Thanks for your help.
 

KitaYama

Well-known member
Local time
Today, 16:29
Joined
Jan 6, 2022
Messages
1,541
Just to clarify for our members who didn't see (or didn't remember) the previous question to which KitaYama referred... the time in question is not the difference between two dates, but the sum of several smaller but non-contiguous (or not necessarily contiguous) intervals.
Thanks Doc for the explanation. I specified the link to previous post in #1 to prevent any further confusion, but it's the holidays and everyone is busy with their family and kids. So there's a good chance they can not review previous posts.

Thanks again and happy new year.
 

Eugene-LS

Registered User.
Local time
Today, 10:29
Joined
Dec 7, 2018
Messages
481
The result is not what I need. Per my example above,
I know ... :)
I forgot to multiply the days by 24 hours.

I already corrected the line
Code:
TimeTo10Base = Val(Mid(sVal, 1, iPos - 1))
to:
Code:
TimeTo10Base = Val(Mid(sVal, 1, iPos - 1)) * 24
... post #4
 
Last edited:

Eugene-LS

Registered User.
Local time
Today, 10:29
Joined
Dec 7, 2018
Messages
481
I didn't expect a user defined function to pass a date as string and split it and work on different parts seperately.
I hoped some vba internal functions can do the magic.
If it's the only way, I will work on user defined function to get the result.
I showed this function only as an example.
You can use similar calculations wherever you want
 

isladogs

MVP / VIP
Local time
Today, 08:29
Joined
Jan 14, 2017
Messages
18,239
Here's an alternative approach, also using a function

Code:
Function ConvertBase10(DayTime As String) As Double

"calculate time in hours

Dim D As Integer, T As Date

'default values
D = 0
T = #12:00:00 AM#

'get the days
If InStr(DayTime, " ") > 0 Then
    D = left(DayTime, InStr(DayTime, " "))
ElseIf InStr(DayTime, ":") = 0 Then
    D = DayTime
End If

'get the time
If InStr(DayTime, ":") > 0 Then
    T = Mid(DayTime, InStr(DayTime, " ") + 1)
End If

'do the math
ConvertBase10 = 24 * (D + CDbl(T))

End Function

Example output
Code:
?ConvertBase10("1 18:30:00")
 42.5

?ConvertBase10("3 3:45:20")
 75.7555555555556

?ConvertBase10("2")
 48

?ConvertBase10("14:00:45")
 14.0125
 

KitaYama

Well-known member
Local time
Today, 16:29
Joined
Jan 6, 2022
Messages
1,541
As I said earlier, I was expecting an internal vba solution.
Now that I have to chop and butcher a time interval as a string to get what I want, I ended up using the following.
For now, the result is what I need.

Code:
Public Function TimeTo10Base(V) As Currency

    Dim t As Date
    Dim d As Single
    If V & "" = "" Then Exit Function

    TimeTo10Base = Split(V, " ")(0) * 24
    t = Split(V, " ")(1)

    d = DateDiff("s", TimeSerial(0, 0, 0), t) / 3600
    TimeTo10Base = TimeTo10Base + Round(d, 2)
 
End Function

Thanks to all who shared their time, wisdom and experience here.
I really appreciate every given advice.
 
Last edited:

ebs17

Well-known member
Local time
Today, 09:29
Joined
Feb 7, 2020
Messages
1,949
Repetition and Compilation:

1) A DateTime value is managed internally as a number of type Double. The integers are the days since Dec 30, 1899, the decimal parts are fractions of a day (1 hour = 1/24, 1 minute = 1/1440, 1second = 1/86400).
Sample calculations
Code:
? #12/30/1899#*1, #1/1/1900 12:00:00#*1, Date*1, Now*1, Format(Now*1,"yyyy-mm-dd hh:nn:ss"), Format(Now*1,"d hh:nn:ss")
0             2,5           44928         44928,4232175926           2023-01-02 10:09:26         2 10:09:26
Since you have a number with a DateTime value, you can calculate directly mathematically, e.g. adding or subtracting or summing.

2) Now comes the big game between value and display of value (view). Nobody can do anything with the double number, so it is presented as a country-specific format for the date and time. So you see a format of the intrinsic value and not the value itself. You can also display a date as a calendar week or quarter or day of the year. There are a few options.
When it comes to formatting, you have to make a big distinction between the format property and the format function. The format property (e.g. for a text field of a form) only changes the display but not the inner value, it remains a number. The format function necessarily creates a string and therefore something else. A string is different from a number and therefore has a different use.

3) For times over 24 hours there is no format that can be used in the property. So you have to switch to the function.
In practical use, it makes little sense to always have to convert between numbers and strings; the computer's processor should deal with important things. It will therefore often make sense to run both the number (for subsequent calculations) and the string (for a view, if required) in parallel in one query for bulk data processing.

4) A DateTime value as a double usually has sufficient accuracy, but double is a floating-point number and therefore has system-related inaccuracies. Time1 = Time2 may be wrong, although the formats shown are identical, since these times could be generated differently and differ in some trailing decimal place. So be careful there.
If you have higher requirements for accuracy, you can have your DateTime value managed over the seconds. For example, the UNIX timestamp counts the seconds since Jan 1 1970. Using DateAdd and DateDiff you can easily convert to the usual form. With the used seconds basis one also gets a practical access to fractions of seconds (milli-/nanoseconds).
 
Last edited:

KitaYama

Well-known member
Local time
Today, 16:29
Joined
Jan 6, 2022
Messages
1,541
@ebs17 Thanks for the detailed and additional explanation. But my main problem is I have to work on interval of time (or elapsed time). Not on date or time itself.
The result of EndingTime - StartingTime is not a date or a time. It's a number that can be in seconds or hours or days.

StartedFrom= 2023/01/01 13:00:00
EndedOn= 2023/01/01 15:30:00

EndedOn - StartedFrom= 2.5 hours OR 9000 seconds

that's all I need.
While a query without any format for the result, shows it as 02:30:00 PM.
To me it makes no sense.
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 08:29
Joined
Feb 19, 2013
Messages
16,618
You have your summed time from your other thread - assuming this is numeric then just multiply by 24 (86400/3600 - number of seconds in a day / number of seconds in an hour)

so 1 day, 1 hour and 15 minutes=1.05208333333

multiply by 24
?(1.05208333333)*24
25.24999999992

but needs rounding to 2 dp

?round((1.05208333333)*24,2)
25.25
 

KitaYama

Well-known member
Local time
Today, 16:29
Joined
Jan 6, 2022
Messages
1,541
@CJ_London I really don't know how to thank you.
Since (per given advice) I had changed the data type of the summed variable to Date, I didn't noticed this solution.
You're really a life saver.
Now I can sleep in peace. No more additional functions.

Thank you my friend.
 

ebs17

Well-known member
Local time
Today, 09:29
Joined
Feb 7, 2020
Messages
1,949
Code:
? #2023-01-01 15:30:00# - #2023-01-01 13:00:00#, DateDiff("s", #1970-01-01#, #2023-01-01 15:30:00#)-DateDiff("s", #1970-01-01#, #2023-01-01 13:00:00#)
0,104166666671517           9000
The first expression shows the resulting double number, which would have to be converted into an understandable format by formatting or conversion. The second expression calculates in seconds.
Code:
? 9000 / (60*60)
 2,5
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 08:29
Joined
Jan 14, 2017
Messages
18,239
I didn't look at your other thread earlier.

If you had just used seconds in your original calculation, it would be simpler still.
For example, a total of 1 day 1 hour and 15 minutes = 86400+3600+900 = 90900 s

90900 / 3600= 25.25
 

KitaYama

Well-known member
Local time
Today, 16:29
Joined
Jan 6, 2022
Messages
1,541
I didn't look at your other thread earlier.

If you had just used seconds in your original calculation, it would be simpler still.
For example, a total of 1 day 1 hour and 15 minutes = 86400+3600+900 = 90900 s

90900 / 3600= 25.25
No unfortunately it's the contents of a textbox with =Sum(Nz([TotalProcessTime],0)) as its Control source
So it shows something like : 1.519375
 

Users who are viewing this thread

Top Bottom