This is ok:
s2:=day-day2;
But this one:
s3:=(years-years2)*365;
It does not account for leap years (years with 366 days)
so s3 should become:
s3 := (years-years2)*365 + LeapYearsOf(years) - LeapYearsOf(years2);
There are two problems with this:
s4:=(mounth-mounth2)*30;
First problem, February is:
28 in normal years
29 in leap years
Second problem, there are 7 months with 31 days.
Simplify the problem by dividing it into small simple problems, like:
How to find out if a year is a leap year:
Leap years happen every four years, but not if the year is a multiple of 100 unless it is also a multiple of 400 as well.
IsLeapYear := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
How many days in full months (or complete months) since the beginning of the year?
The number of full months is equal to Month-1 (or ay - 1 )
Use a small array:
//Days in normal years
MonthDays: array[1..12] of integer =
(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
and a loop from 1 to (Month - 1) or (ay - 1):
DaysOfCompleteMonths := 0;
if ay>1 then
for I := 1 to ay - 1 do Inc(DaysOfCompleteMonths, MonthDays[I])
and if ay>2 and it is a leap year, then add 1:
if ( ay>2 ) and IsLeapYear( yil ) then
Inc(DaysOfCompleteMonths, 1);
This makes s4:
s4 := DaysOfCompleteMonths(yil, ay) - DaysOfCompleteMonths(yil2, ay2);
To calculate LeapYearsOf(Year) to correct s3, use:
LeapYearsOf := Year div 4 - Year div 100 + Year div 400;