Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 316

# Datetime difference [More points]

I have 2 datetime variable . I need to find if the difference between these 2 datetime is greater than 30 seconds . Or in simple , I need to find the difference between 2 times in seconds. It need not to be dattime variable.

It has to takecare even if the first datime and second dattime or of different days .

For eg
t2: 2/1/1900 00:00:10
t1: 1/1/1900 24:59:50

difference 20 seconds

I tried my best like subtracting t2-t1 , multiplying with 10^6 , but it fails when the days changes.
i need to know some function equivalent to DateDiff or MFC TimeSpan

Quite urgent pls . I will give more points for the quickest.

Regards'
Nagarajan

Regards,

0
pamboo
• 4
• 3
• 2
• +1
1 Solution

Author Commented:
correction
======
It has to takecare even if the first datime and second dattime are of different days .
0

Commented:
var
H, M, S, MS: Word
begin
Time1 := StrToDateTime('01/01/1900 23:59:50');
Time2 := StrToDateTime('02/01/1900 00:00:10');
DecodeTime(Time2-Time1 , H, M, S, MS);

now you can just check the values for seconds
0

Commented:
Basically you want to make sure that H, and M are 0, and that S is less than 30 seconds
0

Commented:
If fact you could get away without even decoding

TimeCheck:= StrToTime('00:00:30'); // set what you are trying to compare against ... ie 30 seconds in your case
Time1 := StrToDateTime('01/01/1900 23:59:50');
Time2 := StrToDateTime('02/01/1900 00:00:10');

if (Time2-Time1) > TimeCheck then  // if the difference is greater than 30 secs (and this will work for different days)
0

Commented:
The first one I gave you I didnt correctly finish.
0

Commented:
Hi,

Here it is:

function DateDiffInSecs(Date1, Date2: TDateTime): integer;
begin
Result := Round((Date2-Date1)*86400);
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
var
dt1, dt2: TDateTime;
begin
dt1:= StrToDateTime('1/1/2000 23:59:50');
dt2:= StrToDateTime('2/1/2000 00:00:10');

ShowMessage(IntToStr(DateDiffInSecs(dt1, dt2)));
end;

Regards, Geo
0

Commented:
Hi,

geobul:

I would rather do

function DateDiffInSecs(Date1, Date2: TDateTime): integer;
begin
Result := Trunc((Date2-Date1)*86400);
end;

But it depends of course what pamboo expect for difference for example 29.7 sec.
If he want to count it as 30 sec should be Round
0

Author Commented:
Actually I did like this

DecodeTime(t2-t1,wHour,wMin,wSec,WMili);
if( wSec+wMin*60>nSeconds) then
begin

end

its working . Thanks for ur assistance. Actually I have only the nSeconds to compare . So I must need to decode and get the seconds . The nSeconds cannot be more than 3600 . So I didnt use like this.

Thanks and Regards,
Nagarajan S
0

Commented:
We can make it more incorrect, complicated and slower if you want :-(
0

Author Commented:
GeoBul, answer is more optimal and good . I just saw now only . Really sorry for not  seeing ur solution. I would like to give him points too . I will post one message for points to geobul . please comment and get the points .

Thanks
Nagarajan S
0

## Featured Post

• 4
• 3
• 2
• +1
Tackle projects and never again get stuck behind a technical roadblock.