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

Date functions?

GamonGamon 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.

Comments

  • zibadianzibadian 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]
    The exact parameters and type-casting might be different for FreePascal.
  • GamonGamon Posts: 3Member
    Thanks, and do you happen to know where can I find help file that would integrate with free pascal?
  • zibadianzibadian 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.
  • GamonGamon 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.
  • zibadianzibadian 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.
Sign In or Register to comment.