473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calculating Elapsed Time & then Converting to Decimal

Hi Guys,

I'm looking for a vba code which will calculate the duration of a booking then convert it into a decimal

For Example

If i have a booking Starting on
29/12/2013 22:00

And Ending on 30/12/2013 01:00

Its 4 hours....

So I want a field to display it as 4.00 hours.

Is this possible? A Code would be wonderful. Thanks in advance
Dec 29 '13 #1
3 1753
ADezii
8,834 Expert 8TB
Something along the lines of the following should do the trick:
Expand|Select|Wrap|Line Numbers
  1. Debug.Print FormatNumber(DateDiff("h", <Start Date>, <End Date>), 2) & " hours"
  2. Debug.Print Format$(DateDiff("h", <Start Date>, <End Date>), ".00") & " hours"
Dec 29 '13 #2
I've opted to do it in two functions:

Then I Call the function in my form as RoundToNearest(BookingDuration(ST, ET))
Expand|Select|Wrap|Line Numbers
  1. Public Function BookingDuration(ST As Date, ET As Date) As Double
  2.  
  3. n = DateDiff("n", CDate(ST), CDate(ET))
  4. t = n \ 60 & Format(n Mod 60, "\:00")
  5.  
  6. Dim D As Date, TB, Result As Single
  7.     'For example, it can be at a string
  8.     D = t
  9.     TB = Split(D, ":")
  10.     Result = TB(0) + ((TB(1) * 100) / 60) / 100
  11.  
  12. BookingDuration = Result
  13. End Function
  14.  
  15. Public Function RoundToNearest(Amt As Double) As Double
  16.  
  17. Dim remainder As Double
  18. remainder = Amt - Int(Amt)
  19.  
  20. Dim Temp As Double
  21.     Temp = Amt / 0.25
  22.     If Int(Temp) = Temp Then
  23.         RoundToNearest = Amt
  24.     Else
  25.         Select Case remainder
  26.         Case Is <= 0.125
  27.             remainder = 0
  28.         Case Is <= 0.375
  29.             remainder = 0.25
  30.         Case Is <= 0.625
  31.             remainder = 0.5
  32.         Case Is <= 0.875
  33.             remainder = 0.75
  34.         Case Else
  35.             remainder = 1
  36.         End Select
  37.     RoundToNearest = Int(Amt) + remainder
  38.     End If
  39. End Function
Dec 29 '13 #3
NeoPa
32,556 Expert Mod 16PB
From your question, and how it is asked, the following function will do all you request :
Expand|Select|Wrap|Line Numbers
  1. Public Function BookingDuration(ST As Date, ET As Date) As Double
  2.     BookingDuration = Round((ET - ST) * 24, 2)
  3. End Function
However, looking at your own code, it seems you don't want it rounded to hundredths of an hour as indicated in the question, but rather to the nearest quarter of an hour specified in decimal fractions. That is a little (but not much) more complicated :
Expand|Select|Wrap|Line Numbers
  1. Public Function BookingDuration(ST As Date, ET As Date) As Double
  2.     BookingDuration = Round((ET - ST) * 96, 0) / 4
  3. End Function
Dec 29 '13 #4

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

Similar topics

1
by: NotGiven | last post by:
Below is a good elapsed time function I found. However, I'd like to return total seconds instead of broken down into days, hours, minutes & seconds. In other words, I want "125" instead of "2...
4
by: bonehead | last post by:
Greetings, A friend tells me that he has not been able to work out an expression that calculates elapsed minutes from TimeA to TimeB. For example: TIMEA TIMEB ELAPASED...
20
by: Jean Johnson | last post by:
Hello - I have a start and end time that is written using the following: time.strftime("%b %d %Y %H:%M:%S") How do I calculate the elapsed time? JJ
5
by: techspirit | last post by:
Hi, I am seeking help for calculating the elapsed time on an activity. When the user clicks "pause" button on a form, the timer should also pause. When the user clicks "continue", the timer...
0
by: Anurag | last post by:
Hi, ENV: DB2 ESE 8.2.3 DPF (11 nodes) on AIX 5.x ==== SCENARIO / SETUP ======== ====== (1) I needed to find out CPU Time (User / System) of SQL queries for benchmark testing. (2) I setup...
1
by: oakwoodman | last post by:
I writing a Linux db2 udb sql script, not procedure, and one of the things that I would like to add is the elapsed time that the script runs. I'm having trouble defining start_time, end_time and...
12
by: Spitfire | last post by:
I've a requirement to find the elapsed time between two function calls. I need to find the time elapsed accurate to 1 millisecond. The problem I'm facing right now is that, I'm using the 'time()'...
4
by: Brian | last post by:
I have a 2000/2002 Access db that I use to collect and store my exercisetime using a form to enter. I wanted to see a summary of the total timefor each exercise so I have a subform that does this....
4
by: Pachalo | last post by:
Morning... Am working on a simple billing system and a looking for a code that can help calculate the difference in time between sessions..(am using VB.Net ) This is what is happening: l...
1
by: bklernr | last post by:
Hello, I'm working on a form in Acrobat Pro 9 (not LiveCycle). I need to have an operator enter a beginning time (firsttime) & an end time (secondtime) & when s/he pushes a button (btncalcmix.0),...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
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...

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.