PC Review


Reply
Thread Tools Rate Thread

Calculate time difference

 
 
Kurt
Guest
Posts: n/a
 
      26th Sep 2010
In a query, I need to calculate the elapsed time (in hours and
minutes) between two times (which may span midnight), and round to the
nearest quarter. I will later multiply the elapsed time by an hourly
rate to calculate an amount due.

StartTime and EndTime are both Date/Time fields in Medium Time format.

To calculate the elapsed time, I'm using this:

TotalHrs: Format([StartTime]-1-[EndTime],"Short Time")

But I take it this produces a string (e.g., "01:20"), which means I
can't round it, nor use it in a subsequent calculation. Is there a
better way to go about this? I've tried the DateDif function but if
the difference spans midnight it always miscalculates.

Thanks.
 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      26th Sep 2010
On Sun, 26 Sep 2010 12:24:22 -0700 (PDT), Kurt <(E-Mail Removed)> wrote:

>In a query, I need to calculate the elapsed time (in hours and
>minutes) between two times (which may span midnight), and round to the
>nearest quarter. I will later multiply the elapsed time by an hourly
>rate to calculate an amount due.
>
>StartTime and EndTime are both Date/Time fields in Medium Time format.
>
>To calculate the elapsed time, I'm using this:
>
>TotalHrs: Format([StartTime]-1-[EndTime],"Short Time")
>
>But I take it this produces a string (e.g., "01:20"), which means I
>can't round it, nor use it in a subsequent calculation. Is there a
>better way to go about this? I've tried the DateDif function but if
>the difference spans midnight it always miscalculates.
>
>Thanks.


Use DateDiff instead, to calculate the integer number of minutes:

TotalMin: DateDiff("n", [StartTime], [EndTime])

You can display this as hours and minutes with an expression like

[TotalMin] \ 60 & ":" & Format([TotalMin] MOD 60, "00")

in the control source of a form or report textbox (this won't be editable
though). To calculate total hours you'ld of course divide by 60.
--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/For...-US/accessdev/
http://social.answers.microsoft.com/.../en-US/addbuz/
and see also http://www.utteraccess.com
 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      27th Sep 2010
The following expression can be used to calculate elapsed time when there is
no date component and times may be over two days. For example, 11:49 PM to
1:00 AM

Number of Minutes:
(DateDiff("n",Starttime,Endtime) + 1440) Mod 1440

Number of Seconds:
(DateDiff("s",Starttime,Endtime) + 86400) Mod 86400

You can round (up or down) the number of minutes to the nearest 15 with an
expression like

Round(N/15,0) * 15

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

John W. Vinson wrote:
> On Sun, 26 Sep 2010 12:24:22 -0700 (PDT), Kurt <(E-Mail Removed)> wrote:
>
>> In a query, I need to calculate the elapsed time (in hours and
>> minutes) between two times (which may span midnight), and round to the
>> nearest quarter. I will later multiply the elapsed time by an hourly
>> rate to calculate an amount due.
>>
>> StartTime and EndTime are both Date/Time fields in Medium Time format.
>>
>> To calculate the elapsed time, I'm using this:
>>
>> TotalHrs: Format([StartTime]-1-[EndTime],"Short Time")
>>
>> But I take it this produces a string (e.g., "01:20"), which means I
>> can't round it, nor use it in a subsequent calculation. Is there a
>> better way to go about this? I've tried the DateDif function but if
>> the difference spans midnight it always miscalculates.
>>
>> Thanks.

>
> Use DateDiff instead, to calculate the integer number of minutes:
>
> TotalMin: DateDiff("n", [StartTime], [EndTime])
>
> You can display this as hours and minutes with an expression like
>
> [TotalMin] \ 60 & ":" & Format([TotalMin] MOD 60, "00")
>
> in the control source of a form or report textbox (this won't be editable
> though). To calculate total hours you'ld of course divide by 60.

 
Reply With Quote
 
Kurt
Guest
Posts: n/a
 
      30th Sep 2010
Thank you for the suggestions. It was surprising that date/time
calculations are so difficult and unintuitive.

This is what I ended up doing:

1. Calculate the total number of minutes, which may be over two days:

TotalMin: (DateDiff("n",[Starttime],[Endtime])+1440) Mod 1440

e.g., If StartTime = 9:30 PM and EndTime = 11:50 PM, TotalMin = 140

2. Round the total minutes to the nearest 15 (very unintuitive, since
I want to round the hours to nearest quarter [i.e., 25]. But rounding
the minutes to 15 will get you there):

TotalMinR: Round([TotalMin]/15,0)*15

e.g., 140 -> 135

3. Calculate the number of hours, rounded to the nearest quarter:

TotalHrsR: [TotalMinR]/60

e.g., 135/60 = 2.25

4. Calculate the amount to charge (multiply the total hours by an
hourly rate of $55)

Charge: [TotalHrsR]*55

e.g., 2.25*55 = $146.25

On Sep 27, 11:38*am, John Spencer <JSPEN...@Hilltop.umbc> wrote:
> The following expression can be used to calculate elapsed time when thereis
> no date component and times may be over two days. *For example, 11:49 PM to
> 1:00 AM
>
> Number of Minutes:
> * * (DateDiff("n",Starttime,Endtime) + 1440) Mod 1440
>
> Number of Seconds:
> * * (DateDiff("s",Starttime,Endtime) + 86400) Mod 86400
>
> You can round (up or down) the number of minutes to the nearest 15 with an
> expression like
>
> Round(N/15,0) * 15
>
> John Spencer
> Access MVP 2002-2005, 2007-2010
> The Hilltop Institute
> University of Maryland Baltimore County
>
> John W. Vinson wrote:
> > On Sun, 26 Sep 2010 12:24:22 -0700 (PDT), Kurt <heislerk...@gmail.com> wrote:

>
> >> In a query, I need to calculate the elapsed time (in hours and
> >> minutes) between two times (which may span midnight), and round to the
> >> nearest quarter. I will later multiply the elapsed time by an hourly
> >> rate to calculate an amount due.

>
> >> StartTime and EndTime are both Date/Time fields in Medium Time format.

>
> >> To calculate the elapsed time, I'm using this:

>
> >> TotalHrs: Format([StartTime]-1-[EndTime],"Short Time")

>
> >> But I take it this produces a string (e.g., "01:20"), which means I
> >> can't round it, nor use it in a subsequent calculation. Is there a
> >> better way to go about this? I've tried the DateDif function but if
> >> the difference spans midnight it always miscalculates.

>
> >> Thanks.

>
> > Use DateDiff instead, to calculate the integer number of minutes:

>
> > TotalMin: DateDiff("n", [StartTime], [EndTime])

>
> > You can display this as hours and minutes with an expression like

>
> > [TotalMin] \ 60 & ":" & Format([TotalMin] MOD 60, "00")

>
> > in the control source of a form or report textbox (this won't be editable
> > though). To calculate total hours you'ld of course divide by 60.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate time difference da Microsoft Excel Misc 1 15th Mar 2010 06:45 PM
calculate difference of time =?Utf-8?B?WGF2aWVy?= Microsoft Excel Programming 2 22nd Mar 2006 02:19 PM
Calculate Time Difference =?Utf-8?B?Um9zZW1hcnk=?= Microsoft Access Form Coding 5 28th Feb 2005 12:47 AM
How to calculate a Time Difference =?Utf-8?B?ZHdhbHNoNzc=?= Microsoft Excel Misc 2 10th Nov 2004 09:14 AM
How to calculate time difference? barton Microsoft Access 0 12th Jul 2004 06:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:41 PM.