Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Stumped on elspsed time calculation

 
   Database Forums (Home) -> MS Access RSS
Next:  using Access 2007 as front-end to SQL Server back..  
Author Message
Clif McIrvin

External


Since: May 24, 2008
Posts: 43



(Msg. 1) Posted: Wed Dec 31, 2008 1:18 pm
Post subject: Stumped on elspsed time calculation
Archived from groups: microsoft>public>access (more info?)

I've got a table with two date/time values: [Sample Time] and [Test
Time]

In a query I'm trying to arrive at the elapsed time:

Elapsed Time: [Test Time] - [Sample Time]

The resulting column displays the correct value, ie: 2.42708.... but
when I format it using "d\d h\h" I get "1d 10h" instead of the "2d 10h"
I expected.

Obviously I'm doing something wrong -- please get me pointed in the
right direction!

Thanks!
--
My environment is XP Pro SP2, Access 2003 SP3 w/Hotfix using DAO 3.6.

 >> Stay informed about: Stumped on elspsed time calculation 
Back to top
Login to vote
Pete D.

External


Since: May 10, 2008
Posts: 50



(Msg. 2) Posted: Wed Dec 31, 2008 3:30 pm
Post subject: Re: Stumped on elspsed time calculation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

http://www.everythingaccess.com/tutorials.asp?ID=Calculating-Elapsed-Time

or more precise
From Graham R Seach & Douglas J. Steele
http://www.accessmvp.com/djsteele/Diff2Dates.html
Copy code from WEB page into a modulecompile and save, make sure module name
isn't same as the function name.
In query grid
MyElapsedTimeField: Diff2Dates("dhns",[StartTestField],[EndTestField])

"Clif McIrvin" wrote in message

> I've got a table with two date/time values: [Sample Time] and [Test Time]
>
> In a query I'm trying to arrive at the elapsed time:
>
> Elapsed Time: [Test Time] - [Sample Time]
>
> The resulting column displays the correct value, ie: 2.42708.... but when
> I format it using "d\d h\h" I get "1d 10h" instead of the "2d 10h" I
> expected.
>
> Obviously I'm doing something wrong -- please get me pointed in the right
> direction!
>
> Thanks!
> --
> My environment is XP Pro SP2, Access 2003 SP3 w/Hotfix using DAO 3.6.
>

 >> Stay informed about: Stumped on elspsed time calculation 
Back to top
Login to vote
Clif McIrvin

External


Since: May 24, 2008
Posts: 43



(Msg. 3) Posted: Wed Dec 31, 2008 3:30 pm
Post subject: Re: Stumped on elspsed time calculation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks for the links.

I've discovered that if I use an update query to put the calculated
value into a Date/Time field the "d\d h\h" format gives me the expected
results.

Guess I'll keep the links for future reference, and chalk this one up to
one of those, "Gee, that's odd." moments.

--
Clif

"Pete D." wrote in message

> http://www.everythingaccess.com/tutorials.asp?ID=Calculating-Elapsed-Time
>
> or more precise
> From Graham R Seach & Douglas J. Steele
> http://www.accessmvp.com/djsteele/Diff2Dates.html
> Copy code from WEB page into a modulecompile and save, make sure
> module name isn't same as the function name.
> In query grid
> MyElapsedTimeField: Diff2Dates("dhns",[StartTestField],[EndTestField])
>
> "Clif McIrvin" wrote in message
>
>> I've got a table with two date/time values: [Sample Time] and [Test
>> Time]
>>
>> In a query I'm trying to arrive at the elapsed time:
>>
>> Elapsed Time: [Test Time] - [Sample Time]
>>
>> The resulting column displays the correct value, ie: 2.42708.... but
>> when
>> I format it using "d\d h\h" I get "1d 10h" instead of the "2d 10h" I
>> expected.
>>
>> Obviously I'm doing something wrong -- please get me pointed in the
>> right
>> direction!
>>
>> Thanks!
>> --
>> My environment is XP Pro SP2, Access 2003 SP3 w/Hotfix using DAO 3.6.
>>
>
>
>



--
Clif
 >> Stay informed about: Stumped on elspsed time calculation 
Back to top
Login to vote
Ken Sheridan

External


Since: Jul 16, 2005
Posts: 121



(Msg. 4) Posted: Thu Jan 01, 2009 10:03 am
Post subject: Re: Stumped on elspsed time calculation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Clif:

Its important when undertaking date/time arithmetic to understand how Access
implements the date/time data type. It is in fact as a 64 bit floating point
number of which the integer part represents the days and the fractional part
the times of day. The number is an offset from 30 December 1899 00:00:00,
which is the origin for Access's date/time implementation. So when you
subtract one date/time value from another you'll get the result as the number
of days, in your case 2.42708 days. So far so good.

However, when you format that as days and hours you are not formatting the
time duration, but the date which is represented by the underlying number as
the date of the month (which is what "d" represents) and the hour of the day
on that date.

It happens that 2.42708 is the value which Access stores for the date/time
of 1st January 1900 10:15:00 (Enter ? Format(2.42708, "dd mmmm yyyy
hh:nn:ss") into the debug window to see this for yourself), so the reason you
are getting a result of 1d 10h is because the date is the first of the month.
I can't see how updating another date/time column with the result of the
computation is going to make any difference. The underlying value is still
2.42708, so formatting it as "d\d h\h" should still give you the same result.

Time durations are not date/time values. The latter are points in time.
Its for good reason that the functions in the links to which you've been
referred have been developed, and I'd strongly recommend that you use one of
them, as your current approach is not reliable and will only give a correct
result fortuitously in some cases. For what its worth here's an old function
of my own for computing elapsed time:

Public Function TimeDuration( _
dtmFrom As Date, _
dtmTo As Date, _
Optional blnShowDays As Boolean = False) As String

Const HOURSINDAY = 24
Dim lngHours As Long
Dim strMinutesSeconds As String
Dim strDaysHours As String
Dim dblDuration As Double

dblDuration = dtmTo - dtmFrom

'get number of hours
lngHours = Int(dblDuration) * HOURSINDAY + _
Format(dblDuration, "h")

' get minutes and seconds
strMinutesSeconds = Format(dblDuration, ":nn:ss")

If blnShowDays Then
'get days and hours
strDaysHours = lngHours \ HOURSINDAY & _
" day" & IIf(lngHours \ HOURSINDAY <> 1, "s ", " ") & _
lngHours Mod HOURSINDAY

TimeDuration = strDaysHours & strMinutesSeconds
Else
TimeDuration = lngHours & strMinutesSeconds
End If

End Function

To get the difference between two date/time values expresses as days, hours,
minutes and seconds you'd call it like so:

TimeDuration([Sample Time], [Test Time], True)

Its a simple task to amend the function give the result in your desired
format of course:

Public Function TimeDuration( _
dtmFrom As Date, _
dtmTo As Date) As String

Const HOURSINDAY = 24
Dim lngHours As Long
Dim strDaysHours As String
Dim dblDuration As Double

dblDuration = dtmTo - dtmFrom

'get number of hours
lngHours = Int(dblDuration) * HOURSINDAY + _
Format(dblDuration, "h")

'get days and hours
strDaysHours = lngHours \ HOURSINDAY & "d " & _
lngHours Mod HOURSINDAY & "h"

TimeDuration = strDaysHours

End Function

which you'd call with:

TimeDuration([Sample Time], [Test Time])

Ken Sheridan
Stafford, England

"Clif McIrvin" wrote:

> Thanks for the links.
>
> I've discovered that if I use an update query to put the calculated
> value into a Date/Time field the "d\d h\h" format gives me the expected
> results.
>
> Guess I'll keep the links for future reference, and chalk this one up to
> one of those, "Gee, that's odd." moments.
>
> --
> Clif
>
> "Pete D." wrote in message
>
> > http://www.everythingaccess.com/tutorials.asp?ID=Calculating-Elapsed-Time
> >
> > or more precise
> > From Graham R Seach & Douglas J. Steele
> > http://www.accessmvp.com/djsteele/Diff2Dates.html
> > Copy code from WEB page into a modulecompile and save, make sure
> > module name isn't same as the function name.
> > In query grid
> > MyElapsedTimeField: Diff2Dates("dhns",[StartTestField],[EndTestField])
> >
> > "Clif McIrvin" wrote in message
> >
> >> I've got a table with two date/time values: [Sample Time] and [Test
> >> Time]
> >>
> >> In a query I'm trying to arrive at the elapsed time:
> >>
> >> Elapsed Time: [Test Time] - [Sample Time]
> >>
> >> The resulting column displays the correct value, ie: 2.42708.... but
> >> when
> >> I format it using "d\d h\h" I get "1d 10h" instead of the "2d 10h" I
> >> expected.
> >>
> >> Obviously I'm doing something wrong -- please get me pointed in the
> >> right
> >> direction!
> >>
> >> Thanks!
> >> --
> >> My environment is XP Pro SP2, Access 2003 SP3 w/Hotfix using DAO 3.6.
> >>
> >
> >
> >
>
>
>
> --
> Clif
>
>
>
 >> Stay informed about: Stumped on elspsed time calculation 
Back to top
Login to vote
Clif McIrvin

External


Since: May 24, 2008
Posts: 43



(Msg. 5) Posted: Thu Jan 01, 2009 5:04 pm
Post subject: Re: Stumped on elspsed time calculation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Ken Sheridan" wrote in message

> Clif:
>
> Its important when undertaking date/time arithmetic to understand how
> Access
> implements the date/time data type. It is in fact as a 64 bit
> floating point
> number of which the integer part represents the days and the
> fractional part
> the times of day. The number is an offset from 30 December 1899
> 00:00:00,
> which is the origin for Access's date/time implementation. So when
> you
> subtract one date/time value from another you'll get the result as the
> number
> of days, in your case 2.42708 days. So far so good.
>

<big snip>

Ken -- I never cease to be amazed at your thorough replies. I've learned
a lot from reading your posts.

Thank you!!

Some of what you explained I already knew -- but I was thinking that
"time began on 1/1/1900" so that was part of my confusion. As to why I
seem to be getting the correct (i.e., "expected") results when using a
date/time column instead of a calculated field -- I see what you mean
about not wanting to trust a "fortuitous" occurrence.

Thanks again to all who responded --- I now have a variety of tools to
choose from to solve my dilemma.

--
Clif
 >> Stay informed about: Stumped on elspsed time calculation 
Back to top
Login to vote
John Spencer

External


Since: Feb 10, 2007
Posts: 13



(Msg. 6) Posted: Thu Jan 01, 2009 5:24 pm
Post subject: Re: Stumped on elspsed time calculation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

DateTime value are stored in Access as the number of days and partial
days from midnight Dec 30, 1899.

So showing 2 as a datetime will give you Jan 1, 1900. And Formatting
that to show the day will show you 1 for January ** 1 **

To get a reliable result you could use the DateDiff function to get the
number of minutes and then manipulate that to show the number of days
and hours

DateDiff("h",[Sample Time],[Test Time]) should return the number of hours.

Integer Divide that by 24 to get the number of days and use the modulus
operator to get the remaining hours

DateDiff("h",[Sample Time],[Test Time])\24 & "D " &
DateDiff("h",[Sample Time],[Test Time]) Mod 24

Or use the following untested expression

Int([Test Time] - [Sample Time]) & "D " & Hour([Test Time] - [Sample
Time]) & "H"




'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================


Clif McIrvin wrote:
> I've got a table with two date/time values: [Sample Time] and [Test
> Time]
>
> In a query I'm trying to arrive at the elapsed time:
>
> Elapsed Time: [Test Time] - [Sample Time]
>
> The resulting column displays the correct value, ie: 2.42708.... but
> when I format it using "d\d h\h" I get "1d 10h" instead of the "2d 10h"
> I expected.
>
> Obviously I'm doing something wrong -- please get me pointed in the
> right direction!
>
> Thanks!
 >> Stay informed about: Stumped on elspsed time calculation 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Calculation - Hi, I'm trying to have a field calculate a cost based on the following two fields: (Price_Markup) and (Vendor_Cost). Price Markup is a number and Vendor Cost is a currency. I tried having a simple equation in a query but I kept getting an error..

How can I adapt the Excel calculation for DPMO into Access? - I use {=NORMSINV(1-(# of defects/# of opportunities))+1.5} to calculate DPMO in Excel, but I cannot find a way to translate this calculation onto a form generated from Access. Does anyone know of a way to build this calculation into Access? The..

Time/Counter in Forms - Im wondering is it possible to have a time in a form.. that I can press to start... and then press to pause etc... Its for a game of rugby... Id like it to start at 00:00 and then when i press a button it would start counting up... When it gets to..

time date button - I tutor students in a 1:1 setting online. I wish to track the amount of time I teach the student in each class. Sometimes there are housekeeping issues that need to be address before we begin the actual teaching. So one student will have many classes...

Inputting time values - I have a field where users put times in for the event. It's a text box and they simply type in the value. Does anyone know of a clock control (like the calendar control) that I could put in the form for them to enter a time value? Thanks.
   Database Forums (Home) -> MS Access All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]