# comparing 2 dates, if second date is atleast 24 hours later then...

Hi.
i'm trying to find a way to compare a date/time for a difference of atleast 24 hours (so compare a date i got in a string whit the current date)
i have no clue how to do it though. well i can think of some big mathemetical things =D, but i'm sure theres something smalls i missed.

any suggestions? or could anyone give me a little hint?

REgards,
GoodGrendel

• : Hi.
: i'm trying to find a way to compare a date/time for a difference of atleast 24 hours (so compare a date i got in a string whit the current date)
: i have no clue how to do it though. well i can think of some big mathemetical things =D, but i'm sure theres something smalls i missed.
:
: any suggestions? or could anyone give me a little hint?
:
: REgards,
: GoodGrendel
:

The trouble with time in general is that there are
so many differnet units (second, minute, hour, day,
week, month, year). Another trouble is that same unit
is not always the same (there is something called
leap year, months have different number of days).
Another problem are differnt time zones etc.
This problem was obvious long time ago. Hence some
better systems came with advanced time recording
(everything is stamped in second for example).
Downside is that you have to have two functions
that would do conversions with respect to same reference:
1. Convert current date and time into seconds.
2. Convert any amount of seconds into date and time.

So the first thing is to express both moments in
convenient units (which is second), then subtract
the results and convert difference back to
standard format (year, month, day, hour, minute second).

I hope this helps,

Iby

• [code]
program longlongtime;

type time=record
year, month, day, hour, minute, second:integer;
end;

function time2second(t:time):longint;
const start_day:array[1..12] of integer=
( 1, 32, 60, 90,120,151,181,212,243,273,304,334);
var temp:longint;
begin
temp:=start_day[t.month]+t.day-1;
if t.year>=2000 then begin
if ((t.year mod 4)=0) and (temp>start_day[3])
then temp:=temp+1; { leap year, add one day }
temp:=round(temp+(t.year-2000)*365.25); { days }
temp:=temp*24+t.hour-1; { hours }
temp:=temp*60+t.minute-1; { minutes }
temp:=temp*60+t.second-1; {seconds }
time2second:=temp;
end else time2second:=0;
end;

function more_than_24h(l:longint):boolean;
const one_day:longint=24*60*60; { one hour has 3600 seconds }
begin
more_than_24h:=(l>one_day);
end;

var moment1, moment2, difference:longint;
time1, time2, time3:time;

begin

with time1 do begin
year:=2003; month:=2; day:=18; hour:=13; minute:=15; second:=3;
end;

with time2 do begin
year:=2004; month:=1; day:=1; hour:=21; minute:=23; second:=37;
end;

moment1:=time2second(time1);
moment2:=time2second(time2);

difference:=abs(moment2-moment1);

asm mov ax,3; int 16; end; { just clear that screen }

writeln('Time1 is :',moment1);
writeln('Time2 is :',moment2);
writeln('Diff. is :',difference,' seconds.');

writeln;
writeln('Is it more than one day? ',more_than_24h(difference));

end.

[code]

Please note: I didn't have time to test it, I just trew some
lines together (it might be correct but who knows).

Iby