• Status: Solved
• Priority: Medium
• Security: Public
• Views: 240

# easy question on handling date/time

hi!

is there any procedure/function in delphi which returns the seconds after a specific time? (e.g. how many seconds are between 01.01.1990, 12:00:00 and today?)

I need to compare to dates/time to get the result how many seconds are between two times/dates.

For example i want to know how long it is in seconds between 01.01.01 12:00:00 and 01.01.2001 12:01:38. the problem is how to handle the midnight when the date changes.

martin
0
martinpauly
1 Solution

Commented:
Hi martinpauly,

use TDateTime. It  is just floating number. Ceil part - days, frac part is part of day.
0.5 - is 12 hours
0.25 - 6 hours
so you can substract two dates and it's time
D := D2 - D1;
and then convert it into seconds
SEC := Round(D*24*60*60);

----
Igor.
0

Commented:
Hello,

ITugay is right ;-)

you can use this function:

Function SecBetween(Date1,Date2:tDateTime):Integer;
begin
Result := trunc(date1-date2)*24*3600
end;

Best Regards

Cesario
0

Author Commented:
hi,

the solution you wrote doesn't work. The Result of
date1 - date2 is always 0. So the multiplication with 24 and 3600 doesn't work. to set date1 and date2 i use:

date1:=strtodatetime('01.02.01 23:59:00');
date2:=strtodatetime('01.03.01 00:01:00');
(german date and time settings)

any ideas what i can do?

greetings martin
0

Commented:
Are you sure that you've declared date1 and date2 as TDateTime, not TDate? The function supplied by Cesario should work in this case.
0

Commented:
sect is the number of seconds beetwen 2 dates

var
date1,date2:Tdatetime;
tm,tm1:Ttimestamp;
sect:cardinal;
begin
date1:=strtodatetime('01.02.01 23:59:00');
date2:=strtodatetime('01.03.01 00:01:00');
tm:=datetimetotimestamp(date1);
tm1:=datetimetotimestamp(date2);
sect:=(tm1.Date-tm.Date)*86400+(tm1.time-tm.Time) div 1000;
end;
0

Commented:
Or:

Result := (Date2-Date1)*86400;

If you wish integer number of seconds:

Result := Round((Date2-Date1)*86400);
0

Author Commented:
to stevenB: yes i'm sure i use tdatetime to declare

to  DMN: your solution doesn't work! same problem i wrote above.

to f15iaf : your right! thx a lo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.