# Dates in Turbo Pascal

I'm trying to write a program in Turbo Pascal for Windows that will calcualte the difference in days between two dates, among other things to do with dates. I'm trying to do this as a function that can take the two dates as six integers (day1, day2, month1, month2, year1, year2) and return an integer which is the number of days.

This isn't so much a pascal specific question I'm asking, but I'm trying (and failing) to do this in Pascal...can anyone help me please? Thanks.

-Dave

• [b][red]This message was edited by zibadian at 2003-2-13 14:55:31[/red][/b][hr]
: I'm trying to write a program in Turbo Pascal for Windows that will calcualte the difference in days between two dates, among other things to do with dates. I'm trying to do this as a function that can take the two dates as six integers (day1, day2, month1, month2, year1, year2) and return an integer which is the number of days.
:
: This isn't so much a pascal specific question I'm asking, but I'm trying (and failing) to do this in Pascal...can anyone help me please? Thanks.
:
: -Dave
:
The first step is to count the number of days from a certain start date (for example: jan 1, 0000). Here is a start of a function which does that:
[code]
function EncodeDate(day, month, year: integer): longint;
begin
Year := Year - 1; { Remove current year }
EncodeDate := Year * 365 + Year div 4 - Year div 100 + Year div 400;
{ Calculate the number of days based on the year only }
{ This code also keeps track of leap years }
end;
[/code]
This function still needs to take into account the number of days for each month and the days, which shouldn't be too difficult to code.

Once you know this number of days, you can simply subtract the two numbers to get the final result:
[code]
function DateDifference(day1, day2, month1, month2, year1, year2: integer): integer;
var
Date1, Date2: Longint;
begin
Date1 := EncodeDate(day1, month1, year1); { First encode dates }
Date2 := EncodeDate(day2, month2, year2);
Result := Abs(Date2 - Date1); { Then subtract them }
end;
[/code]