473,493 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

yarbrough40
320 Contributor
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 15278
yarbrough40
320 Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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 Contributor
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

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2474
by: D. Shane Fowlkes | last post by:
Here's a good one. I've been using an Excel spreadsheet for the past couple of years to calculate a file's Estimated Download Time based off of a solid 50kbs connection (dial up). This is for a...
2
6922
by: Robert Brown | last post by:
Hi all. I have written a program that transfers data via FTP. I can calculate the rough amount of time left. I get a value such as 70.83 seconds left for instance. What I want to do is to...
14
4054
by: Michael Barrido | last post by:
I have this for example: Dim iSeconds as int32 = 3600 '3600 seconds is one hour How do i convert it to "01:00:00" ? Please help. Thanks in advance!
5
10523
by: kpp9c | last post by:
Hi, I was looking at python & datetime and hoping that it would already have a method/func to translate time formats. I need to translate seconds to hh:mm:ss.ms and vice versa and would like...
5
19156
nirmalsingh
by: nirmalsingh | last post by:
i am getting date format in string as 30-11-2006 05:59:44 PM . i want to convert in the format of yyyy-mm-dd hh:mm:ss to sore it in mysql database. Help me with sample coding plz..
1
2664
seshu
by: seshu | last post by:
Hi every body to morning my cousine has show his application and also his live db in that to sav the length of all the voice files he has saved in time format ie he took the datatype of time now i...
1
2137
seshu
by: seshu | last post by:
Hi every body to morning my cousine has show his application and also his live db in that to sav the length of all the voice files he has saved in time format ie he took the datatype of time now i...
5
27081
by: lofty | last post by:
Hi, I'm building a time report system for work and I want to show the total hours and minutes a project have taken but can't get it to work ... I have a database (MySQL) in one table I have...
5
32278
by: sudip77 | last post by:
Hi, I have an user input variable called @stop_time which takes input in the format of HH:MM:SS. I need to convert it into seconds only. How can I do that? Can anyone help please?
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.