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
3 1753
Something along the lines of the following should do the trick: - Debug.Print FormatNumber(DateDiff("h", <Start Date>, <End Date>), 2) & " hours"
-
Debug.Print Format$(DateDiff("h", <Start Date>, <End Date>), ".00") & " hours"
I've opted to do it in two functions:
Then I Call the function in my form as RoundToNearest(BookingDuration(ST, ET)) - Public Function BookingDuration(ST As Date, ET As Date) As Double
-
-
n = DateDiff("n", CDate(ST), CDate(ET))
-
t = n \ 60 & Format(n Mod 60, "\:00")
-
-
Dim D As Date, TB, Result As Single
-
'For example, it can be at a string
-
D = t
-
TB = Split(D, ":")
-
Result = TB(0) + ((TB(1) * 100) / 60) / 100
-
-
BookingDuration = Result
-
End Function
-
-
Public Function RoundToNearest(Amt As Double) As Double
-
-
Dim remainder As Double
-
remainder = Amt - Int(Amt)
-
-
Dim Temp As Double
-
Temp = Amt / 0.25
-
If Int(Temp) = Temp Then
-
RoundToNearest = Amt
-
Else
-
Select Case remainder
-
Case Is <= 0.125
-
remainder = 0
-
Case Is <= 0.375
-
remainder = 0.25
-
Case Is <= 0.625
-
remainder = 0.5
-
Case Is <= 0.875
-
remainder = 0.75
-
Case Else
-
remainder = 1
-
End Select
-
RoundToNearest = Int(Amt) + remainder
-
End If
-
End Function
NeoPa 32,556
Expert Mod 16PB
From your question, and how it is asked, the following function will do all you request : - Public Function BookingDuration(ST As Date, ET As Date) As Double
-
BookingDuration = Round((ET - ST) * 24, 2)
-
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 : - Public Function BookingDuration(ST As Date, ET As Date) As Double
-
BookingDuration = Round((ET - ST) * 96, 0) / 4
-
End Function
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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()'...
|
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....
|
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...
|
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),...
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
| |