Hours and minutes calculations

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm looking for the best way to calculate time in hours and minutes from two datetime fields, i.e., "04/01/2004 2300" and "04/02/2004 0635." I would like the results displayed on the form immediately, but not necessarily stored. Additionally, the code should lend itself to the calculation of total hours and minutes (not days, weeks or years) in a summary report. Any ideas as to what might be the optimum approach using T-SQL in a project file? Thanks for any ideas.
 
Joe;

Don't know if this is the best way, but it is a way! I'd use the Sql
DateDiff() function. Here is an example.

declare @startDT as datetime
declare @endDT as datetime

set @startDT = cast('04/01/2004 23:00' as datetime)
set @endDT = cast('04/02/2004 06:35' as datetime)

Select @startDT as StartDate,
@endDT as EndDate,
datediff(hh,@startDT,@endDT) as Hrs,
datediff(n,@startDT,@endDT) - (datediff(hh,@startDT,@endDT)*60) as mins

When run it returns:

StartDate End Date
Hrs mins
2004-04-01 23:00:00.000 2004-04-02 06:35:00.000 7 35

Ron W


Joe said:
I'm looking for the best way to calculate time in hours and minutes from
two datetime fields, i.e., "04/01/2004 2300" and "04/02/2004 0635." I would
like the results displayed on the form immediately, but not necessarily
stored. Additionally, the code should lend itself to the calculation of
total hours and minutes (not days, weeks or years) in a summary report. Any
ideas as to what might be the optimum approach using T-SQL in a project
file? Thanks for any ideas.
 
Back
Top