468,727 Members | 1,564 Online

# VB Function to convert Decimal to Elapsed Time [h]:mm:ss (like in Excel)?

320 100+
Can anyone help me to display a Decimal (or double) to elapsed time [h]:mm:ss (hours:minutes:seconds)using vb.net?
example:
if you were to type "1.34666666666667" into a cell in Excel then choose Custom Format [h]:mm:ss The result is an elapsed time of "32:19:12". Using the OADate function only runs on a 24 hour clock so this does not work for longer durations unfortunately...
Jun 21 '09 #1
4 14468
yarbrough40
320 100+
ok so I figured it out with math I don't normally have patience for so in case anyone cares... I created these two simple functions produce the result

Expand|Select|Wrap|Line Numbers
1. Private Function ElapsedTime(ByVal DNum As Double) As String
2.
3.         Dim hh, mm, ss As Integer
4.         Dim _hh, _mm, _ss As Double
5.
6.         _hh = DNum * 24
7.         hh = Math.Truncate(_hh)
8.
9.         _mm = DecimalPart(_hh) * 60
10.         mm = Math.Truncate(_mm)
11.
12.         _ss = DecimalPart(_mm) * 60
13.         ss = Math.Truncate(_ss)
14.
15.
16.         ElapsedTime = hh & ":" & mm & ":" & ss
17.
18.         Return ElapsedTime
19.     End Function
20.

Expand|Select|Wrap|Line Numbers
1. Private Function DecimalPart(ByVal nbr As Decimal) As Double
2.
3.         Dim wholePart, fractionalPart As Decimal
4.
5.         wholePart = Math.Truncate(nbr)
6.         fractionalPart = nbr - wholePart
7.
8.         DecimalPart = fractionalPart
9.         Return DecimalPart
10.
11.     End Function
12.
Jun 22 '09 #2
iam_clint
1,208 Expert 1GB
You can use a stopwatch to do elapsed time...

Can you explain better what you are actually trying to accomplish
Jun 22 '09 #3
iam_clint
1,208 Expert 1GB
i'm sure there is a better way to accomplish what you are actually trying to get can you get a little more detail on what you are doing
Jun 22 '09 #4
yarbrough40
320 100+
I litterally have a database with elapsed times (in double format) so I have no need to use a stopwatch since I already have the elapsed times. I simply need to display those elapsed times on the page in the format of [h]:mm:ss

the functions I wrote do work. if u have a better suggestion that would be excellent, however I don't believe there is an existing function for this...

example: 1.34666666666667
A) multiply the entire decimal by 24 (the whole part of the answer is hours) = 32
B) multiply the entire decimal from (A) and multiply it by 60(the whole part is the minutes) = 19
C) multiply the entire decimal from (B) and multiply it by 60(the whole part is the seconds) = 12

put em all together separated by ":" and there u have it
"32:19:12"
Jun 22 '09 #5