Date functions? - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Date functions?

Posts: 3Member
Hi everyone, I am new at Pascal and I was wondering how do I go about making date comparison and substraction for this year. For exapmle if I enter two dates(just month and day), one after another and then I get how much days is difference between them?
Could somebody please write down the main code for this?
I downloaded Free Pascal-24MB, and it doesn't have any help files integrated.

• Posts: 6,349Member
: Hi everyone, I am new at Pascal and I was wondering how do I go about making date comparison and substraction for this year. For exapmle if I enter two dates(just month and day), one after another and then I get how much days is difference between them?
: Could somebody please write down the main code for this?
: I downloaded Free Pascal-24MB, and it doesn't have any help files integrated.
:
Dates are stored as reals in Pascal. The integer part is the number of days after a set "zero-day", while the fractional part is the time within that day. Thus calculating the difference between 2 dates is just as easy as calculating the difference between 2 reals. Here is an example, which is based on TP and Delphi:
[code]
Date1 := EncodeDate(Day1, Month1, Year1);
Date2 := EncodeDate(Day2, Month2, Year2);
ResultDate := Date2 - Date1;
DecodeDate(ResultDate, ResultDay, ResultMonth, ResultYear);
[/code]
• Posts: 3Member
Thanks, and do you happen to know where can I find help file that would integrate with free pascal?
• Posts: 6,349Member
: Thanks, and do you happen to know where can I find help file that would integrate with free pascal?
:
I don't know. I've always used the various pascal compilers made by Borland. Perhaps on their website and otherwise you might find it using google.
• Posts: 3Member
I tried it in Turbo Pascal 7.0, I wrote this:

Program Date;
var date1,date2,ResultDate,EncodeDate,DecodeDate:real;
begin
date1:=EncodeDate(day1,Month1,Year1);
date2:=EncodeDate(day2,Month2,Year2);
ResultDate:=Date2-Date1;
DecodeDate(ResultDate,ResultDay,ResultMonth,ResultYear);
writeln(ResultDate);
end.

and it doesn't work!

For example, if I enter two dates in this format- 14.7 and 27.9 i should get time difference in days(one integer), and it should check if correct input is made, so I can't input 41.13 or something like that.
• Posts: 6,349Member
: I tried it in Turbo Pascal 7.0, I wrote this:
:
: Program Date;
: var date1,date2,ResultDate,EncodeDate,DecodeDate:real;
: begin
: date1:=EncodeDate(day1,Month1,Year1);
: date2:=EncodeDate(day2,Month2,Year2);
: ResultDate:=Date2-Date1;
: DecodeDate(ResultDate,ResultDay,ResultMonth,ResultYear);
: writeln(ResultDate);
: end.
:
: and it doesn't work!
:
: For example, if I enter two dates in this format- 14.7 and 27.9 i should get time difference in days(one integer), and it should check if correct input is made, so I can't input 41.13 or something like that.
:
I didn't know that TP 7 doesn't have EncodeDate()/DecodeDate().
The way to calculate it, is to first calculate the difference to the 1st day of the next month. Example: 14-7 to 01-8. For this you need to have a list of they days per month for each month.
Then you can loop through the months, adding the days per month together, until you reach the 1st day of the last month (ie 01-9). Finally you calculate the difference between that day and the final day.
As for validation, first check if the month is within the legal values. Then check if the day is within the legal values, based on the days per month.