# Board index » delphi » Calculating Difference Between Dates?

## Calculating Difference Between Dates?

DateTime values are coming in from SQL Server.

Is there a Delphi function that will return to time difference of two
date/times?

Example:
01/01/01 15:00 and 01/01/01  17:00
Looking for a function that would return 2 hours?

## Re:Calculating Difference Between Dates?

Whats wrong with:
datediff := onedate - otherdate;
where they are all TDateTime variables?

If you want to format it, use the FormatDateTime function.

Christopher Latta http://www.ozemail.com.au/~clatta
In theory, there is no difference between theory and practice,
but in practice there is a great deal of difference.

##### Quote
Dan wrote in message <3a81b867\$1_2@dnews>...
>DateTime values are coming in from SQL Server.

>Is there a Delphi function that will return to time difference of two
>date/times?

>Example:
>01/01/01 15:00 and 01/01/01  17:00
>Looking for a function that would return 2 hours?

## Re:Calculating Difference Between Dates?

Dan,

There is the MSSQL function DateDiff

Select DateDiff(hh,'01/01/2001 15:00','01/01/2001 17:00')

Check out the Books Online for other date/time functions.

Good luck,
krf

##### Quote
Dan wrote in message <3a81b867\$1_2@dnews>...
>DateTime values are coming in from SQL Server.

>Is there a Delphi function that will return to time difference of two
>date/times?

>Example:
>01/01/01 15:00 and 01/01/01  17:00
>Looking for a function that would return 2 hours?

## Re:Calculating Difference Between Dates?

##### Quote
> Whats wrong with:
> datediff := onedate - otherdate;
> where they are all TDateTime variables?

If I take '01/01/01 15:00' - '01/02/01 17:00' the Delphi answer is '12/31/99
2:00AM'

The answer I'm looking for would be 26 hours. . .

Dan

## Re:Calculating Difference Between Dates?

Since I'm sure someone out there has done this I'll ask a different way. . .
.

I'm writing a timeclock program. . .SQL stores Delphi's NOW when employee
clocks in/out. . .

Now it is time to figure how many hours that employee worked using a
StartTime and EndTime (both TDateTime vars). . .

I know I can grunt work it out - but I'm trying to work smarter if you know
what I mean. . .

Regards,
Dan

## Re:Calculating Difference Between Dates?

##### Quote
Dan wrote in message <3a81cfe5\$1_2@dnews>...

>I'm writing a timeclock program. . .SQL stores Delphi's NOW when
employee
>clocks in/out. . .

>Now it is time to figure how many hours that employee worked using a
>StartTime and EndTime (both TDateTime vars). . .

>I know I can grunt work it out - but I'm trying to work smarter if you
know
>what I mean. . .

EarlierDateTime := LaterDateTime - EarlierDateTime ;
DecodeDate(EarlierDateTime, yy, mnth, dd);
DecodeTime(EarlierDateTime, hh, mm, ss, ms);

--
Wayne Niddery (WinWright Inc.)
"No matter what happens, somebody will find a way to take it too
seriously" - Dave Barry

## Re:Calculating Difference Between Dates?

Try

var
hours : Double;
onedate, otherdate : TDateTime;
begin
hours := (onedate - otherdate) * 24;
end;

You would need to decide what to do with the minutes, of course.

A TDateTime variable is just a float that represents days + the fraction of
a day since 12/30/1899

For instance, at the time of wrinting this, "Now" is equal to
36929.9038009954, which means 36,929 days since 12/30/1899 and 0.9038...
part of a day (about 9:43 PM). If I had written this at noon, "Now" would
have returned 36929.5

--

Alain Quesnel
cinqsanss...@compuserve.com

##### Quote
"Dan" <d...@emscoelectric.com> wrote in message news:3a81ca1f_2@dnews...

> > Whats wrong with:
> > datediff := onedate - otherdate;
> > where they are all TDateTime variables?

> If I take '01/01/01 15:00' - '01/02/01 17:00' the Delphi answer is
'12/31/99
> 2:00AM'

> The answer I'm looking for would be 26 hours. . .

> Dan

## Re:Calculating Difference Between Dates?

Hours:double;
Starttime, EndTime:tDateTime;

begin

//the times are stored as floats with each whole day as an integer
//and the time of day as the decimal.
//let's say he came in a 8AM and left at 5PM.
//StartTime would be the day integer number + 8/24ths of a day (.33333).
//EndTime would be the Day Number + 17/24ths
//     (5pm is 17 o'clock) of a day .7083333).
// or the answer would be .375 (of a day) or 9/24ths.
// to get hours, all you do is multiply by 24 which gives 9.
// You CAN use formatdatetime, but the date information
//has been subtracted out.
// just use formatdatetime('hh:nn',Hours);
// The beauty of this is that it works for grave shift too,
// where the employee came to work at 11PM Monday and left at
// 8AM Tuesday.  For instance starting at 1 23/24 and ending at
// 2 8/24 (or 1.958333 and 2.333333) gives .375 of a day or
// 8 hours.

Hours:=(EndTime-StartTime)*24;

end;

##### Quote
Dan wrote:
> Since I'm sure someone out there has done this I'll ask a different way. . .
> .

> I'm writing a timeclock program. . .SQL stores Delphi's NOW when employee
> clocks in/out. . .

> Now it is time to figure how many hours that employee worked using a
> StartTime and EndTime (both TDateTime vars). . .

> I know I can grunt work it out - but I'm trying to work smarter if you know
> what I mean. . .

> Regards,
> Dan

--

MOST OF THE TIME I SEND MESSAGES TO THE PERSON OR ORGANIZATION I
INTENDED TO SEND THEM TO.  WHEN THAT HAPPENS, FEEL FREE TO GO AHEAD
AND READ THEM.  IF THIS IS NOT THE PERSON I INTENDED TO SEND TO, PLEASE
DISCARD THIS MESSAGE.  IF YOU AREN'T SURE WHETHER THIS MESSAGE IS FOR

Bryan Valencia
bry...@eloan.com
E-LOAN Inc. - A Better way to get a loan.

## Re:Calculating Difference Between Dates?

##### Quote
Bryan Valencia wrote:
>     // where the employee came to work at 11PM Monday and left at
>     // 8AM Tuesday.  For instance starting at 1 23/24 and ending at
>     // 2 8/24 (or 1.958333 and 2.333333) gives .375 of a day or
>     // 8 hours.

OOPS I mean 9 hours and sorry for posting HTML.  I am {*word*132} myself
now.

## Re:Calculating Difference Between Dates?

BTW, if you use FormatDateTime with a value that represents 25 hours (or
more), it will return '01', since the 24 hours represent one day.
--

Alain Quesnel
cinqsanss...@compuserve.com

##### Quote
"Bryan Valencia" <br...@209software.com> wrote in message

news:3A82FFB0.4938AFA0@209software.com...
Hours:double;
Starttime, EndTime:tDateTime;
begin
//the times are stored as floats with each whole day as an integer
//and the time of day as the decimal.
//let's say he came in a 8AM and left at 5PM.
//StartTime would be the day integer number + 8/24ths of a day (.33333).
//EndTime would be the Day Number + 17/24ths
//     (5pm is 17 o'clock) of a day .7083333).
// or the answer would be .375 (of a day) or 9/24ths.
// to get hours, all you do is multiply by 24 which gives 9.
// You CAN use formatdatetime, but the date information
//has been subtracted out.
// just use formatdatetime('hh:nn',Hours);
// The beauty of this is that it works for grave shift too,
// where the employee came to work at 11PM Monday and left at
// 8AM Tuesday.  For instance starting at 1 23/24 and ending at
// 2 8/24 (or 1.958333 and 2.333333) gives .375 of a day or
// 8 hours.

Hours:=(EndTime-StartTime)*24;
end;

##### Quote
Dan wrote:

Since I'm sure someone out there has done this I'll ask a different way. . .
.
I'm writing a timeclock program. . .SQL stores Delphi's NOW when employee
clocks in/out. . .
Now it is time to figure how many hours that employee worked using a
StartTime and EndTime (both TDateTime vars). . .
I know I can grunt work it out - but I'm trying to work smarter if you know
what I mean. . .
Regards,
Dan
--
MOST OF THE TIME I SEND MESSAGES TO THE PERSON OR ORGANIZATION I
INTENDED TO SEND THEM TO.  WHEN THAT HAPPENS, FEEL FREE TO GO AHEAD
AND READ THEM.  IF THIS IS NOT THE PERSON I INTENDED TO SEND TO, PLEASE
DISCARD THIS MESSAGE.  IF YOU AREN'T SURE WHETHER THIS MESSAGE IS FOR